将活动传递给我的自定义适配器类时,活动为空

时间:2013-04-02 11:38:02

标签: android

当我传递给我的自定义适配器时,

Acvitity为空。当我将方向从纵向更改为横向或反之亦然时会发生这种情况。

我调试了,但无法弄清楚为什么传递给我的Adapter类的Activity为null。

贝娄是完整的片段类。

public class FragmentClass extends SherlockListFragment implements AsyncListner {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        AsyncTaskClass task = new AsyncTaskClass (Object, getSherlockActivity());
        task.setListener(this);
        task.execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.documents, container, false);
    }

    @Override
    public void onLoadComplete(List < Response > data) {
        AdapterClass adapter = new AdapterClass (getSherlockActivity(),
            android.R.id.list, data);
        setListAdapter(adapter);
    }

}

以下是日志:

>     E / AndroidRuntime(1142): FATAL EXCEPTION: main E / AndroidRuntime(1142): java.lang.NullPointerException E /
> AndroidRuntime(1142): at
> android.widget.ArrayAdapter.init(ArrayAdapter.java: 310) E /
> AndroidRuntime(1142): at android.widget.ArrayAdapter. < init >
> (ArrayAdapter.java: 153) E / AndroidRuntime(1142): at
> com.example.test.DocumentsAdapter. < init > (DocumentsAdapter.java:
> 22) E / AndroidRuntime(1142): at
> com.example.test.DocumentsFragment.onLoadComplete(DocumentsFragment.java:
> 121) E / AndroidRuntime(1142): at
> com.example.test.DocumentsAsyncTask.onPostExecute(DocumentsAsyncTask.java:
> 101) E / AndroidRuntime(1142): at
> com.example.test.DocumentsAsyncTask.onPostExecute(DocumentsAsyncTask.java:
> 1) E / AndroidRuntime(1142): at
> android.os.AsyncTask.finish(AsyncTask.java: 631) E /
> AndroidRuntime(1142): at
> android.os.AsyncTask.access$600(AsyncTask.java: 177) E /
> AndroidRuntime(1142): at
> android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:
> 644) E / AndroidRuntime(1142): at
> android.os.Handler.dispatchMessage(Handler.java: 99) E /
> AndroidRuntime(1142): at android.os.Looper.loop(Looper.java: 137) E /
> AndroidRuntime(1142): at
> android.app.ActivityThread.main(ActivityThread.java: 4745) E /
> AndroidRuntime(1142): at java.lang.reflect.Method.invokeNative(Native
> Method) E / AndroidRuntime(1142): at
> java.lang.reflect.Method.invoke(Method.java: 511) E /
> AndroidRuntime(1142): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
> 786) E / AndroidRuntime(1142): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 553) E /
> AndroidRuntime(1142): at dalvik.system.NativeStart.main(Native Method)

DocumentsAdapter adapter = new DocumentsAdapter(getSherlockActivity(),                 android.R.id.list,data);

我将它作为参数传递给我的Adapter类时得到getSherlockActivity() = null

1 个答案:

答案 0 :(得分:5)

当片段与活动分离时,会将其活动引用设置为null。因此,您应该在片段的onAttach()方法中保存对活动的引用:

private SherlockActivity mActivity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = getSherlockActivity();
}

@Override
public void onLoadComplete(List<DocumentResponse> data) {
    DocumentsAdapter adapter =
            new DocumentsAdapter(mActivity, android.R.id.list, data);
    setListAdapter(adapter);
}

或在实际访问活动之前检查活动是否不是null

@Override
public void onLoadComplete(List<DocumentResponse> data) {
    SherlockActivity activity = getSherlockActivity();
    if (activity != null) {
        DocumentsAdapter adapter =
                new DocumentsAdapter(activity, android.R.id.list, data);
        setListAdapter(adapter);
    }
}