错误:在显示java.lang.NullPointerException android.support.v4.content.Loader [英] Error: java.lang.NullPointerException at android.support.v4.content.Loader

查看:580
本文介绍了错误:在显示java.lang.NullPointerException android.support.v4.content.Loader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施 LoaderManager 在我的应用程序,以减少启动时间的正如你可以看到这里但在该线程执行的建议,我是收到以下错误 initLoader(INT,捆绑后, LoaderManager.LoaderCallbacks)在类型LoaderManager不适用于参数(INT,空,MainActivity)。在这里搜索后,我发现对于第二个错误解决这里。但是从实施后的建议后,现在我得到的错误显示java.lang.NullPointerException
在android.support.v4.content.Loader
。我把了code,我现在有,谁能告诉我,我怎么能解决这个问题。

FULL code可以看出 这里

登录猫

  20 10-03:11:34.849:E / AndroidRuntime(2968):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.test.sms.it/com。 test.sms.it.MainActivity}:显示java.lang.NullPointerException
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.os.Handler.dispatchMessage(Handler.java:99)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.os.Looper.loop(Looper.java:123)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 20:11:34.849:E / AndroidRuntime(2968):在java.lang.reflect.Method.invokeNative(本机方法)
10-03 20:11:34.849:E / AndroidRuntime(2968):在java.lang.reflect.Method.invoke(Method.java:507)
10-03 20:11:34.849:E / AndroidRuntime(2968):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 20:11:34.849:E / AndroidRuntime(2968):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 20:11:34.849:E / AndroidRuntime(2968):在dalvik.system.NativeStart.main(本机方法)
10-03 20:11:34.849:E / AndroidRuntime(2968):因:显示java.lang.NullPointerException
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.support.v4.content.Loader<&初始化GT;(Loader.java:91)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.support.v4.content.AsyncTaskLoader<&初始化GT;(AsyncTaskLoader.java:92)
10-03 20:11:34.849:E / AndroidRuntime(2968):在com.test.sms.it.MainActivity $ ContactsDataLoader<&初始化GT;(MainActivity.java:104)
10-03 20:11:34.849:E / AndroidRuntime(2968):在com.test.sms.it.MainActivity.onCreateLoader(MainActivity.java:496)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.support.v4.app.LoaderManagerImpl.createLoader(LoaderManager.java:487)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:496)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:550)
10-03 20:11:34.849:E / AndroidRuntime(2968):在com.test.sms.it.MainActivity.onCreate(MainActivity.java:91)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-03 20:11:34.849:E / AndroidRuntime(2968):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

MainActivity.java

 进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.Map;
进口android.app.AlertDialog;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.database.Cursor;
进口android.graphics.Color;
进口android.net.ConnectivityManager;
进口android.net.NetworkInfo;
进口android.net.Uri;
进口android.os.AsyncTask;
进口android.os.Bundle;
。进口的Andr​​oid preference preferenceManager。
进口android.provider.ContactsContract;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.LoaderManager;
进口android.support.v4.content.AsyncTaskLoader;
进口android.support.v4.content.Loader;
进口android.view.KeyEvent;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.Window;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.AutoCompleteTextView;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.SimpleAdapter;
进口android.widget.Spinner;
进口android.widget.Toast;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.activity_main);
    editUser1UserName =(EditText上)findViewById(R.id.editTextUser1UserName);
    editUser1Password =(EditText上)findViewById(R.id.editTextUser1Password);
    editUser2UserName =(EditText上)findViewById(R.id.editTextUser2UserName);
    editUser2Password =(EditText上)findViewById(R.id.editTextUser2Password);
    editUser3UserName =(EditText上)findViewById(R.id.editTextUser3UserName);
    editUser3Password =(EditText上)findViewById(R.id.editTextUser3Password);
    editUser4UserName =(EditText上)findViewById(R.id.editTextUser4UserName);
    editUser4Password =(EditText上)findViewById(R.id.editTextUser4Password);
    editUser5UserName =(EditText上)findViewById(R.id.editTextUser5UserName);
    editUser5Password =(EditText上)findViewById(R.id.editTextUser5Password);
    mTxtPhoneNo =(AutoCompleteTextView)findViewById(R.id.mmWhoNo);
    mTxtPhoneNo.setThreshold(1);
    mTxtPhoneNo.setTextColor(Color.BLACK);
    EDITTEXT =(EditText上)findViewById(R.id.editTextMessage);
    spinner1 =(微调)findViewById(R.id.spinnerGateway);
    btnsend =(按钮)findViewById(R.id.btnSend);
    btnContact =(按钮)findViewById(R.id.btnContact);
    btnClear =(按钮)findViewById(R.id.btnClear);
    btnClear.setOnClickListener(本);
    btnsend.setOnClickListener(本);
    btnContact.setOnClickListener(本);
    mPeopleList =新的ArrayList<地图<字符串,字符串>>();
    getSupportLoaderManager()initLoader(0,null,则此)。
    mTxtPhoneNo.setOnItemClickListener(本);
    readPerson();
    面板面板;
    topPanel = =面板(面板)findViewById(R.id.mytopPanel);
    panel.setOnPanelListener(本);
    panel.setInterpolator(新BounceInterpolator(Type.OUT));
    getLoginDetails();
}公共类ContactsDataLoader扩展
        AsyncTaskLoader< ArrayList的<地图<字符串,字符串>>> {
    公共ContactsDataLoader(上下文的背景下){
        超级(上下文);
    }    @覆盖
    公众的ArrayList<地图<字符串,字符串>> loadInBackground(){
        mPeopleList.clear();
        光标人= getContentResolver()查询(
                ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,
                空值);
        而(people.moveToNext()){
            字符串CONTACTNAME =人
                    .getString(人
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            字符串的ContactID = people.getString(人
                    .getColumnIndex(ContactsContract.Contacts._ID));
            字符串hasPhone =人
                    .getString(人
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));            如果((的Integer.parseInt(hasPhone)0)){
                光标手机= getContentResolver()查询(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        空值,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                +=+的ContactID,NULL,NULL);
                而(phones.moveToNext()){
                    字符串phoneNumber的=手机
                            .getString(电话
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    字符串numberType =手机
                            .getString(电话
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    地图<字符串,字符串> NamePhoneType =新的HashMap<字符串,字符串>();
                    NamePhoneType.put(名,联系人姓名);
                    NamePhoneType.put(手机,phoneNumber的);
                    如果(numberType.equals(0))
                        NamePhoneType.put(类型,工作);
                    否则如果(numberType.equals(1))
                        NamePhoneType.put(类型,家);
                    否则如果(numberType.equals(2))
                        NamePhoneType.put(类型,手机);
                    其他
                        NamePhoneType.put(类型,其他);
                    mPeopleList.add(NamePhoneType);
                }
                phones.close();
            }
        }
        people.close();
        startManagingCursor(人);
        返回mPeopleList;
    }    @覆盖
    保护无效onStartLoading(){
        super.onStartLoading();
        的forceload();
    }
}公共装载机< ArrayList的<地图<字符串,字符串>>> onCreateLoader(INT ID,
        捆绑参数){
    返回新ContactsDataLoader(背景);
}公共无效onLoadFinished(装载机< ArrayList的<地图<字符串,字符串>>>装载机,
        ArrayList的<地图<字符串,字符串>>数据){
    mPeopleList =数据;
    mAdapter =新SimpleAdapter(这一点,mPeopleList,R.layout.custcontview,
            新的String [] {姓名,手机,类型},新的INT [] {
                    R.id.ccontName,R.id.ccontNo,R.id.ccontType});
    mTxtPhoneNo.setAdapter(mAdapter);
}公共无效onLoaderReset(装载机< ArrayList的<地图<字符串,字符串>>>装载机){
    mPeopleList =新的ArrayList<地图<字符串,字符串>>();
    mAdapter =新SimpleAdapter(这一点,mPeopleList,R.layout.custcontview,
            新的String [] {姓名,手机,类型},新的INT [] {
                    R.id.ccontName,R.id.ccontNo,R.id.ccontType});
    mTxtPhoneNo.setAdapter(mAdapter);
}


解决方案

RC 的评论是你的问题的答案。你传递一个 上下文(因为我没有看到任何地方code您发布的背景可变正与一个有效的参考初始化)到您的自定义加载程序和code将无法用 NullPointerException异常。如果你从我的previous答案用我的code,那么这些回调在活动实施。在这种情况下,只是通过这个,提到当前的活动例如,您的自定义加载器:

 公共装载机< ArrayList的<地图<字符串,字符串>>> onCreateLoader(INT ID,
        捆绑参数){
    返回新ContactsDataLoader(本);
}

另外,如果你关闭游标,就没有必要使用 startManagingCursor()光标作为你可能会惹上麻烦。

I want to implement LoaderManager in my application in order to reduce the startup time As you can see here but after implementing the suggestion in that thread i was getting the following error initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, MainActivity). After searching on here i found the solution for that second error here. But after implementing the suggestion from later, now i am getting the error java.lang.NullPointerException at android.support.v4.content.Loader. I am putting up the code which i currently have, can anyone tell me, how can i solve this problem

FULL CODE CAN BE SEEN HERE

Log Cat

10-03 20:11:34.849: E/AndroidRuntime(2968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.sms.it/com.test.sms.it.MainActivity}: java.lang.NullPointerException
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.os.Looper.loop(Looper.java:123)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at java.lang.reflect.Method.invoke(Method.java:507)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at dalvik.system.NativeStart.main(Native Method)
10-03 20:11:34.849: E/AndroidRuntime(2968): Caused by: java.lang.NullPointerException
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.support.v4.content.Loader.<init>(Loader.java:91)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.support.v4.content.AsyncTaskLoader.<init>(AsyncTaskLoader.java:92)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at com.test.sms.it.MainActivity$ContactsDataLoader.<init>(MainActivity.java:104)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at com.test.sms.it.MainActivity.onCreateLoader(MainActivity.java:496)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.support.v4.app.LoaderManagerImpl.createLoader(LoaderManager.java:487)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:496)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:550)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at com.test.sms.it.MainActivity.onCreate(MainActivity.java:91)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-03 20:11:34.849: E/AndroidRuntime(2968):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

MainActivity.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    editUser1UserName = (EditText) findViewById(R.id.editTextUser1UserName);
    editUser1Password = (EditText) findViewById(R.id.editTextUser1Password);
    editUser2UserName = (EditText) findViewById(R.id.editTextUser2UserName);
    editUser2Password = (EditText) findViewById(R.id.editTextUser2Password);
    editUser3UserName = (EditText) findViewById(R.id.editTextUser3UserName);
    editUser3Password = (EditText) findViewById(R.id.editTextUser3Password);
    editUser4UserName = (EditText) findViewById(R.id.editTextUser4UserName);
    editUser4Password = (EditText) findViewById(R.id.editTextUser4Password);
    editUser5UserName = (EditText) findViewById(R.id.editTextUser5UserName);
    editUser5Password = (EditText) findViewById(R.id.editTextUser5Password);
    mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
    mTxtPhoneNo.setThreshold(1);
    mTxtPhoneNo.setTextColor(Color.BLACK);
    editText = (EditText) findViewById(R.id.editTextMessage);
    spinner1 = (Spinner) findViewById(R.id.spinnerGateway);
    btnsend = (Button) findViewById(R.id.btnSend);
    btnContact = (Button) findViewById(R.id.btnContact);
    btnClear = (Button) findViewById(R.id.btnClear);
    btnClear.setOnClickListener(this);
    btnsend.setOnClickListener(this);
    btnContact.setOnClickListener(this);
    mPeopleList = new ArrayList<Map<String, String>>();
    getSupportLoaderManager().initLoader(0, null, this);
    mTxtPhoneNo.setOnItemClickListener(this);
    readPerson();
    Panel panel;
    topPanel = panel = (Panel) findViewById(R.id.mytopPanel);
    panel.setOnPanelListener(this);
    panel.setInterpolator(new BounceInterpolator(Type.OUT));
    getLoginDetails();
}

public class ContactsDataLoader extends
        AsyncTaskLoader<ArrayList<Map<String, String>>> {
    public ContactsDataLoader(Context context) {
        super(context);
    }

    @Override
    public ArrayList<Map<String, String>> loadInBackground() {
        mPeopleList.clear();
        Cursor people = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null,
                null);
        while (people.moveToNext()) {
            String contactName = people
                    .getString(people
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String hasPhone = people
                    .getString(people
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if ((Integer.parseInt(hasPhone) > 0)) {
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    String phoneNumber = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String numberType = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    Map<String, String> NamePhoneType = new HashMap<String, String>();
                    NamePhoneType.put("Name", contactName);
                    NamePhoneType.put("Phone", phoneNumber);
                    if (numberType.equals("0"))
                        NamePhoneType.put("Type", "Work");
                    else if (numberType.equals("1"))
                        NamePhoneType.put("Type", "Home");
                    else if (numberType.equals("2"))
                        NamePhoneType.put("Type", "Mobile");
                    else
                        NamePhoneType.put("Type", "Other");
                    mPeopleList.add(NamePhoneType);
                }
                phones.close();
            }
        }
        people.close();
        startManagingCursor(people);
        return mPeopleList;
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }
}

public Loader<ArrayList<Map<String, String>>> onCreateLoader(int id,
        Bundle args) {
    return new ContactsDataLoader(context);
}

public void onLoadFinished(Loader<ArrayList<Map<String, String>>> loader,
        ArrayList<Map<String, String>> data) {
    mPeopleList = data;
    mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview,
            new String[] { "Name", "Phone", "Type" }, new int[] {
                    R.id.ccontName, R.id.ccontNo, R.id.ccontType });
    mTxtPhoneNo.setAdapter(mAdapter);
}

public void onLoaderReset(Loader<ArrayList<Map<String, String>>> loader) {
    mPeopleList = new ArrayList<Map<String, String>>();
    mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview,
            new String[] { "Name", "Phone", "Type" }, new int[] {
                    R.id.ccontName, R.id.ccontNo, R.id.ccontType });
    mTxtPhoneNo.setAdapter(mAdapter);
}

解决方案

RC's comment is the answer of your problem. You pass a null Context(as I don't see anywhere in the code you posted the context variable being initialized with a valid reference) to your custom loader and the code will fail with a NullPointerException. If you used my code from my previous answer then those callbacks are implemented in your Activity. In this case just pass this, the reference to your current Activity instance, to your custom loader:

public Loader<ArrayList<Map<String, String>>> onCreateLoader(int id,
        Bundle args) {
    return new ContactsDataLoader(this);
}

Also, if you close the cursor, there is no need to use startManagingCursor() for that Cursor as you might get in trouble.

这篇关于错误:在显示java.lang.NullPointerException android.support.v4.content.Loader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆