尝试启动我的应用程序时出现NullPointerException

时间:2014-04-23 15:56:01

标签: java android sharedpreferences logcat

我正在尝试运行我的主菜单,该菜单会创建一个SharedPreference文件,其中包含一个ArrayList,其中包含字符串和双打的对象Food。 在尝试运行它时,应用程序崩溃了, 这是我的代码:

package com.example.davidson;

import java.util.ArrayList;

import com.google.gson.Gson;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainMenu extends ActionBarActivity implements OnClickListener {
Button btn1,btn2,btn3,btn4;
public ArrayList<Food>L=new ArrayList<Food>();
public static String filename="mySharedList";
SharedPreferences myFoodList;
public Editor editor=myFoodList.edit();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);    
    myFoodList=getSharedPreferences(filename,0);
    btn1=(Button)findViewById(R.id.button1);
    btn2=(Button)findViewById(R.id.button2);
    btn3=(Button)findViewById(R.id.button3);
    btn4=(Button)findViewById(R.id.button4);
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    Createfoods();



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_menu,
                container, false);
        return rootView;
    }
}

@Override
public void onClick(View v) {
    if(v.getId()==R.id.button1)
    {
        Intent openFoodSearch=new      Intent("com.example.davidson.FOODSEARCH");
        startActivity(openFoodSearch);
    }
    if(v.getId()==R.id.button2)
    {
        Intent openMyList=new Intent("com.example.davidson.MY_LIST");
        startActivity(openMyList);
    }
    if(v.getId()==R.id.button3)
    {
        Intent openRecommended=new     Intent("com.example.davidson.RECOMMENDED");
        startActivity(openRecommended);
    }
    if(v.getId()==R.id.button4)
    {
        Intent openAddItem=new Intent("com.example.davidson.ADDITEM");
        startActivity(openAddItem);
    }

    }
public void Createfoods()
{
Food f1=new Food("cucumber",50,5,4,3);
Food f2=new Food("tomato",50,3,2,1);
L.add(f1);
L.add(f2);
Gson gson=new Gson();
String json=gson.toJson(L);
editor.putString("myShardList",json);
editor.commit();

}
}

下面是logcat

Shutting down VM
   threadid=1: thread exiting with uncaught exception (group=0xb3a87ba8)
    FATAL EXCEPTION: main
   Process: com.example.davidson, PID: 1157
    java.lang.RuntimeException: Unable to instantiate activity           ComponentInfo{com.example.davidson/com.example.davidson.MainMenu}:     java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
    at com.example.davidson.MainMenu.<init>(MainMenu.java:26)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1208)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
    ... 11 more
       Sending signal. PID: 1157 SIG: 9

1 个答案:

答案 0 :(得分:3)

由于这些行,你得到NullPointerException

SharedPreferences myFoodList;
public Editor editor=myFoodList.edit();

您无法调用edit() myFoodList函数,因为它尚未初始化且为null。您需要先进行初始化,然后才能调用它,最好是在onCreate()函数中。

您可以通过查看Logcat输出来解决这个问题,特别是这些行:

Caused by: java.lang.NullPointerException
  at com.example.davidson.MainMenu.<init>(MainMenu.java:26)

logcat告诉您问题在MainMenu.java中 26