gpt4 book ai didi

android - 错误 : java. lang.NullPointerException 在 android.support.v4.content.Loader

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:24 25 4
gpt4 key购买 nike

<分区>

我想在我的应用程序中实现 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
在 android.support.v4.content.Loader
。我正在贴出我目前拥有的代码,谁能告诉我,我该如何解决这个问题

完整代码可见 HERE

日志猫

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);
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com