LoaderManager和java.lang.NullPointerException错误

时间:2012-08-08 09:09:28

标签: android android-fragments android-loadermanager

我正在研究android中的碎片,我写这个简单的程序: 在这个应用程序中我想创建一个列表视图,我不会创建一个后台线程,这个线程在日志文件中写入

这是主要的活动

public class MainActivity extends FragmentActivity {
    public static final String TAG = "Test_Loader"; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "Start  !!!!!!!!!!!!!!");    
        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new listFragment()).commit();

        }
    }
}

这是我的ListFragment:

public class listFragment extends ListFragment implements LoaderCallbacks<Void> {

    public static final String TAG = "Test_Loader";
    private ProgressBar mProgressView;
    private LinearLayout mFormView;
    private int valTest = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, null);
        Log.i(TAG, "start listFragment");
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        ArrayList<Map<String, String>> list = buildData();
        String[] from = { "name", "purpose" };
        int[] to = { R.id.recordTitolo, R.id.recordDescrizione };
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), list,
                R.layout.record, from, to);
        setListAdapter(adapter);

        /** IL LOADERMANAGER */
        getLoaderManager().initLoader(0, null, this);

    }


    private ArrayList<Map<String, String>> buildData() {
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(putData("android", "mobile"));
        list.add(putData("winphone7 ", "windows"));
        list.add(putData("iPhone", "iPhone"));
        return list;
        // TODO Auto-generated method stub

    }


    private HashMap<String, String> putData(String name, String purpose) {
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("name", name);
        item.put("purpose", purpose);
        return item;
    }


    @Override
    public Loader<Void> onCreateLoader(int id, Bundle args) {
        Log.i(TAG, "onCreateLoader(): id=" + id);
        AsyncTaskLoader<Void> loader = new AsyncTaskLoader<Void>(getActivity()) {
            @Override
            public Void loadInBackground() {

                try {

                Log.i(TAG, "loadInBackground(): doing some work....");
                    for (int i = 0; i < 4; ++i) {
                    Log.i(TAG, "backgroud: number " + i);
                    }
                    valTest = 100;
                    Thread.sleep(5000);                                     
                } catch (InterruptedException e) {
                    Log.d(TAG, "ERROR" + e);
                }

                Log.i(TAG, "Stop backgroud");
                return null;
            }
        };
        Log.i(TAG, "return");
        loader.forceLoad();
        return loader;
    }

    // metodo per implementare dopo caricato questo metido viene richiamato
    @Override
    public void onLoadFinished(Loader<Void> arg0, Void arg1) {
        toggleLoading(false);
        buildForm();
        // TODO Auto-generated method stub

    }
    private void buildForm() {

        // TODO Auto-generated method stub
        Log.i(TAG, "Valuet: " + valTest);
    }

    private void toggleLoading(boolean b) {
        // TODO Auto-generated method stub
        mProgressView.setVisibility(b ? View.VISIBLE : View.GONE); 
        mFormView.setGravity(b ? Gravity.CENTER : Gravity.TOP); 
    }

    @Override
    public void onLoaderReset(Loader<Void> arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG, "start onLoaderReset()");
    }

}

在这里你可以看到activity_main.xml

activity_main.xml

现在我认为问题存在于getLoaderManager中,但我不明白如何解决它。

修改

当我运行我的应用程序时,我在logcat中看到了这一点:

08-08 15:13:09.347: E/AndroidRuntime(341): FATAL EXCEPTION: main
08-08 15:13:09.347: E/AndroidRuntime(341): java.lang.NullPointerException
08-08 15:13:09.347: E/AndroidRuntime(341):  at com.example.test3.listFragment.toggleLoading(listFragment.java:128)
08-08 15:13:09.347: E/AndroidRuntime(341):  at com.example.test3.listFragment.onLoadFinished(listFragment.java:114)
08-08 15:13:09.347: E/AndroidRuntime(341):  at com.example.test3.listFragment.onLoadFinished(listFragment.java:1)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:425)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:393)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.content.Loader.deliverResult(Loader.java:103)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 15:13:09.347: E/AndroidRuntime(341):  at android.os.Looper.loop(Looper.java:123)

我认为问题是这个代码

private void toggleLoading(boolean b) {
    // TODO Auto-generated method stub
    mProgressView.setVisibility(b ? View.VISIBLE : View.GONE); 
    mFormView.setGravity(b ? Gravity.CENTER : Gravity.TOP);
}

当我删除此代码时,一切正常,但我不知道,因为我有这种情况。

1 个答案:

答案 0 :(得分:1)

您遇到了这个问题,因为您似乎没有初始化mProgressViewmFormView字段。如果它们在布局中定义,你会想要这样的东西:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_main, null);

    mProgressView = (ProgressBar)view.findViewById(R.id.progress_view_id);
    mFormView = (LinearLayout)view.findViewById(R.id.form_view_id);

    Log.i(TAG, "start listFragment");
    return view;
}