更新:我通过使用解决了我的问题
android:configChanges="orientation|screenSize"
当我尝试更改方向时,我的应用程序崩溃了,我查看了其他几个线程,但似乎并没有解决我的问题。
我试图添加
android:configChanges="keyboardHidden|orientation|screenLayout"
它也不起作用。
从 logcat 来看,这似乎是由 onTextChangedListener 部分引起的,但我不知道为什么会导致问题。
05-02 01:13:45.132: D/AndroidRuntime(20683): Shutting down VM
05-02 01:13:45.132: W/dalvikvm(20683): threadid=1: thread exiting with uncaught exception (group=0x40afb930)
05-02 01:13:48.235: E/AndroidRuntime(20683): FATAL EXCEPTION: main
05-02 01:13:48.235: E/AndroidRuntime(20683): java.lang.RuntimeException: Unable to start activity ComponentInfo{..../....Music}: java.lang.NullPointerException
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ActivityThread.access$700(ActivityThread.java:153)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.os.Looper.loop(Looper.java:137)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ActivityThread.main(ActivityThread.java:5226)
05-02 01:13:48.235: E/AndroidRuntime(20683): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 01:13:48.235: E/AndroidRuntime(20683): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 01:13:48.235: E/AndroidRuntime(20683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-02 01:13:48.235: E/AndroidRuntime(20683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-02 01:13:48.235: E/AndroidRuntime(20683): at dalvik.system.NativeStart.main(Native Method)
05-02 01:13:48.235: E/AndroidRuntime(20683): Caused by: java.lang.NullPointerException
05-02 01:13:48.235: E/AndroidRuntime(20683): at ....Music$2.onTextChanged(Music.java:628)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.widget.TextView.sendOnTextChanged(TextView.java:7231)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.widget.TextView.setText(TextView.java:3699)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.widget.TextView.setText(TextView.java:3554)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.widget.EditText.setText(EditText.java:80)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.widget.TextView.setText(TextView.java:3529)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.widget.TextView.onRestoreInstanceState(TextView.java:3429)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.view.View.dispatchRestoreInstanceState(View.java:12093)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2588)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2588)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.view.View.restoreHierarchyState(View.java:12071)
05-02 01:13:48.235: E/AndroidRuntime(20683): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1634)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.Activity.onRestoreInstanceState(Activity.java:938)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ListActivity.onRestoreInstanceState(ListActivity.java:219)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.Activity.performRestoreInstanceState(Activity.java:910)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1131)
05-02 01:13:48.235: E/AndroidRuntime(20683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2275)
05-02 01:13:48.235: E/AndroidRuntime(20683): ... 12 more
可能导致问题的线路在这里(我认为)。
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"] - Start ["+start+"] - Before ["+before+"] - Count ["+count+"]");
if (count < before) {
// We're deleting char so we need to reset the adapter data
((MyMediaAdapter) adapter).resetData();
}
adapter.getFilter().filter(s.toString()); <-- THIS IS LINE 628
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
更新:不确定我的 AsyncTask 是否与它有关。所以我就把它贴在这里。
private class AsyncFetchMedia extends AsyncTask<String, Void, String> {
String toLoad;
public AsyncFetchMedia (String toLoad) {
this.toLoad = toLoad;
}
@Override
protected String doInBackground(String... params) {
try {
// Fetching data
} catch (Exception e) {
e.printStackTrace();
}
return "Completed Loading " + toLoad + " Music List";
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
adapter = new MyMediaAdapter(Music.this, getMediaList);
setListAdapter(adapter);
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(Music.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
让我知道是否需要更多信息。