Android 应用程序因方向更改而崩溃

Android 应用程序因方向更改而崩溃,android,android-edittext,orientation,Android,Android Edittext,Orientation,更新:我通过使用 android:configChanges="orientation|screenSize" 当我试图改变方向时,我的应用程序崩溃了,我查看了其他几个线程,但它似乎并没有解决我的问题 我已经试着加进去了 android:configChanges="keyboardHidden|orientation|screenLayout" 而且它也不起作用 从日志来看,它似乎是由onTextChangedListener部分引起的,但我不知道它为什么会引起这个问题 05-02 01:

更新:我通过使用

android:configChanges="orientation|screenSize"
当我试图改变方向时,我的应用程序崩溃了,我查看了其他几个线程,但它似乎并没有解决我的问题

我已经试着加进去了

android:configChanges="keyboardHidden|orientation|screenLayout"
而且它也不起作用

从日志来看,它似乎是由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(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
System.out.println(“文本[“+s+”]-Start[“+Start+”]-Before[“+Before+”]-Count[“+Count+”]);
如果(计数<之前){
//我们正在删除字符,因此需要重置适配器数据
((MyMediaAdapter)adapter.resetData();
}

adapter.getFilter().filter(s.toString());您的适配器在哪里初始化?@Tom inside AsyncTask in onPostExecute part.OK-在我看来,您的适配器是空的,因为它在方向更改后没有正确初始化…即onCreate()因为当你改变方向时,你的应用程序会被破坏并恢复…如果没有你的主要活动,很难判断code@Tom我用android:configChanges=“orientation | screenSize”,screenSize,而不是screenLayout解决了这个问题,就像我以前做的那样,现在它可以工作了。谢谢你的帮助。
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) {
            }
        });
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) {

        }
    }