Android developers (the first line of code Second Edition) common exceptions and solutions (based on Android Studio) (b)

1.Glide:You must pass in a non null View

In the Customize Dialog when using Glide Load picture Times a bit abnormal
Caused by: java.lang.IllegalArgumentException: You must pass in a non null View
because Dialog has not been displayed, and the ImageView is null, it is reported that this anomaly
the solution is to put Dialog displayed, mDialog.show (); go in with Glide load picture.

2.Android studio project crashes reported Binary XML file line # 2: Error inflating class type I error

Possible Cause: Can not find resource file: the system will load the resources to choose a different drawable folder according to the resolution, if only in a decentralized file a resource file, a different resolution of the device error.
Specific reference https://www.cnblogs.com/awkflf11/p/5362927.html and https://www.cnblogs.com/longmaoxiansheng/p/9420619.html

3.android.content.ActivityNotFoundException: No Activity found to handle Intent 问题

Given as follows

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.DEFAULT] dat=content://***.fileProvider/files_root/Android/data/***/cache/ofddata/5bd4483f46db4ea58fe3e7a0cb387cf8.ofd typ=application/ofd flg=0x10000003 }

This is because the activity did not write the new configuration information in the configuration file AndroidManifest.xml inside,
solution:
related activities in AndroidManifest.xml of acitivity add intent-filter, as follows

<intent-filter>
	<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Another reason may be that the profile information clerical error

<activity
    android:name="com.example.message.Message"
    android:label="@string/title_activity_message_web"
    android:theme="@android:style/Theme.NoTitleBar" >
</activity>

As above, the file name may be inconsistent with the actual label property, should be extra careful.

4. Android messaging mechanism in solution: Only the original thread that created a view hierarchy can touch its views

Original code

public class MainActivity extends Activity implements View.OnClickListener {
	
	private TextView stateText;
	private Button btn;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        stateText = (TextView) findViewById(R.id.tv);
        btn = (Button) findViewById(R.id.btn);
        
        btn.setOnClickListener(this);
    }
 
	@Override
	public void onClick(View v) {
		new WorkThread().start();
	}
	
	//工作线程
	private class WorkThread extends Thread {
		@Override
		public void run() {
			//......处理比较耗时的操作
			
			//处理完成后改变状态
			stateText.setText("completed");
		}
	}
}

Runtime will complain

ERROR/AndroidRuntime(421): FATAL EXCEPTION: Thread-8
ERROR/AndroidRuntime(421): android.view.ViewRoot$CalledFromWrongThreadException: 
Only the original thread that created a view hierarchy can touch its views.

The reason is that the view component is not thread-safe Android system, if you want to update the view must be updated in the main thread, the update operation can not be performed in the sub-thread.
Solution: notify the main thread in the child thread, the main thread to do the update operation and use Handler object notify the main thread.

public class MainActivity extends Activity implements View.OnClickListener {
	
	private static final int COMPLETED = 0;
	
	private TextView stateText;
	private Button btn;
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == COMPLETED) {
				stateText.setText("completed");
			}
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        stateText = (TextView) findViewById(R.id.tv);
        btn = (Button) findViewById(R.id.btn);
        
        btn.setOnClickListener(this);
    }
 
	@Override
	public void onClick(View v) {
		new WorkThread().start();
	}
	
	//工作线程
	private class WorkThread extends Thread {
		@Override
		public void run() {
			//......处理比较耗时的操作
			
			//处理完成后给handler发送消息
			Message msg = new Message();
			msg.what = COMPLETED;
			handler.sendMessage(msg);
		}
	}
}

Through the above this way, we can solve thread safety issues, the complex task of processing work to the child thread to complete, and then notify the main thread by thread sub-handler object by the main thread update the view, the process from the message mechanism an important role.
※ Android to implement the message loop mechanism by Looper, Handler. Android news cycle is for the threads, each thread can have its own message queue and message loop.
For more information about the mechanism of analysis can refer https://blog.csdn.net/mars2639/article/details/6625165# .

5. After the Welcome screen to start the APP settings, AndroidStudio add boot screen flash back

Solution:
The start of the first page is set to XXXActivity, at the same time, add the original file to its main activities the following:

<activity android:name=".XXXActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>

Be able to operate after the change.

6.Handler.SendMessage()——Cannot resolve method 'sendMessage(android.os.Message)

Cause: Import Error packages; -
Solution: will import java.util.logging.Handler;be changed import android.os.Handler;to.

Published 51 original articles · won praise 184 · views 30000 +

Guess you like

Origin blog.csdn.net/CUFEECR/article/details/103341465