尝试更改方向时出现空指针异常

时间:2014-05-12 14:38:14

标签: android android-fragments android-orientation

更改布局时空指针异常。我用了片段。

当我尝试在布局中定位时。我使用公共类MainFragment扩展了SherlockFragment

下面的Logcat 错误如:

05-12 14:38:27.676: E/AndroidRuntime(6773): FATAL EXCEPTION: main
05-12 14:38:27.676: E/AndroidRuntime(6773): java.lang.NullPointerException
05-12 14:38:27.676: E/AndroidRuntime(6773):     at com.android.aa.adapter.VideoListAdapter.<init>(VideoListAdapter.java:47)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at com.android.aa.tv.MainFragment$AsyncAction.onPostExecute(MainFragment.java:134)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at com.android.aa.tv.MainFragment$AsyncAction.onPostExecute(MainFragment.java:1)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at android.os.AsyncTask.finish(AsyncTask.java:590)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at android.os.AsyncTask.access$600(AsyncTask.java:149)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at android.os.Looper.loop(Looper.java:132)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at android.app.ActivityThread.main(ActivityThread.java:4123)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at java.lang.reflect.Method.invoke(Method.java:491)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-12 14:38:27.676: E/AndroidRuntime(6773):     at dalvik.system.NativeStart.main(Native Method)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //setRetainInstance(true);

        rootView = inflater.inflate(R.layout.fragment_main,container,false);
        initView();
        return rootView;
}

private void initView() { 

    new AsyncAction().execute(null, null, null);

}

((ListView)rootView.findViewById(R.id.videoListView))。setAdapter(new VideoListAdapter(getActivity(),aryThumbnail,aryUrl,aryTitle,aryDesc));

private class AsyncAction extends AsyncTask<String, Void, String> 
{           
    protected String doInBackground(String... arg0) 
    {
         callRecentVideo();
         return null;
    }

    protected void onPostExecute(String result) 
    {
         pd.dismiss();Log.e("length",String.valueOf(aryThumbnail.size()));
         ((ListView)rootView.findViewById(R.id.videoListView)).setAdapter(new VideoListAdapter(getActivity(),aryThumbnail,aryUrl,aryTitle,aryDesc));
    }

    protected void onPreExecute() 
    {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = new ProgressDialog(getActivity());
        pd.setTitle("app");
        pd.setMessage("Please wait while we are loading..");
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.show();
    }
}

2 个答案:

答案 0 :(得分:0)

问题是你再次重新创建片段并执行asyntask,这将导致nullpointer ..

解决方案每次更改方向时都需要保持asyntask活着。

有一个很好的实施here;

答案 1 :(得分:0)

我在清单中解决了类似的问题:

    <activity
        android:name="<package-name>"
        android:label="<app-name>" 
        android:configChanges="orientation|keyboardHidden|screenSize">
    </activity>