我想在我的应用程序中实现LoaderManager
以减少启动时间As you can see here,但在该线程中实现建议后,我收到以下错误initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, MainActivity)
。在这里搜索后,我找到了第二个错误here的解决方案。但在实施后来的建议后,现在我收到错误java.lang.NullPointerException
at android.support.v4.content.Loader
。我正在提供我目前拥有的代码,任何人都可以告诉我,我该如何解决这个问题
可以看到完整的代码 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);
}
答案 0 :(得分:1)
RC
的评论是您问题的答案。您传递null
Context
(因为我在您发布的context
变量的代码中没有看到任何用有效引用初始化的代码)到您的自定义加载器,代码将失败一个NullPointerException
。如果您使用了我之前回答的代码,那么这些回调就会在Activity
中实现。在这种情况下,只需将this
对当前Activity
实例的引用传递给自定义加载器:
public Loader<ArrayList<Map<String, String>>> onCreateLoader(int id,
Bundle args) {
return new ContactsDataLoader(this);
}
此外,如果您关闭光标,则无需使用startManagingCursor()
Cursor
,因为您可能遇到麻烦。