问题
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);
}
回答1:
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.
来源:https://stackoverflow.com/questions/12792953/error-java-lang-nullpointerexception-at-android-support-v4-content-loader