gpt4 book ai didi

android - 为什么我会在这一行收到 NullPointerException?

转载 作者:行者123 更新时间:2023-11-29 16:04:04 24 4
gpt4 key购买 nike

我的代码以前运行良好,现在抛出 NPE。它也不在一条新线上,所以我很困惑。有人能指出我正确的方向吗?

问题线在大约一半的位置被识别出来。

public class FavouritesActivity extends Activity {

private String mFavourites;
ArrayAdapter <String> adapter;
List<String> list;

private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favourites);

lv = (ListView) findViewById(R.id.My_Favourites);

SharedPreferences preferences = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
list = new ArrayList<String>();
Map<String, ?> prefsMap = preferences.getAll();
for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
list.add(entry.getValue().toString()); //THIS IS THE PROBLEM <<<<<<<<<<<<<<<<<<
}

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

lv.setAdapter(adapter);

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
// setting onItemLongClickListener and passing the position to the function
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
String mFavourites =(lv.getItemAtPosition(position).toString());
removeItemFromList(position);

return true;
}
});

}

protected void removeItemFromList(int position) {


final int deletePosition = position;

AlertDialog.Builder alert = new AlertDialog.Builder(
FavouritesActivity.this);

alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub

// main code on after clicking yes
list.remove(deletePosition);

Operator();


adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();


}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});


alert.show();


}

protected void Operator() {
SharedPreferences preferences = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();

editor.remove(mFavourites);
editor.commit();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_supplements, menu);
return true;
}
}

这是 LogCat:

01-10 14:08:59.850: E/AndroidRuntime(2194): FATAL EXCEPTION: main
01-10 14:08:59.850: E/AndroidRuntime(2194): Process: com.LifeSchematics.msg, PID: 2194
01-10 14:08:59.850: E/AndroidRuntime(2194): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.LifeSchematics.msg/com.LifeSchematics.msg.FavouritesActivity}: java.lang.NullPointerException
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.os.Looper.loop(Looper.java:136)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-10 14:08:59.850: E/AndroidRuntime(2194): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 14:08:59.850: E/AndroidRuntime(2194): at java.lang.reflect.Method.invoke(Method.java:515)
01-10 14:08:59.850: E/AndroidRuntime(2194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-10 14:08:59.850: E/AndroidRuntime(2194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-10 14:08:59.850: E/AndroidRuntime(2194): at dalvik.system.NativeStart.main(Native Method)
01-10 14:08:59.850: E/AndroidRuntime(2194): Caused by: java.lang.NullPointerException
01-10 14:08:59.850: E/AndroidRuntime(2194): at com.LifeSchematics.msg.FavouritesActivity.onCreate(MySupplementsActivity.java:40)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.Activity.performCreate(Activity.java:5231)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-10 14:08:59.850: E/AndroidRuntime(2194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

最佳答案

您会得到 NullPointerException,因为某些首选项值存储为 null 而 entry.getValue() 为 null。所以在调用 toString() 之前添加 null 检查

for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
final Object value = entry.getValue();
list.add(value == null ? "null" : value.toString());
}

或者

for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
final Object value = entry.getValue();
if (value != null) {
list.add(value.toString());
}
}

关于android - 为什么我会在这一行收到 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21052601/

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