我在Android中的MainMenu.class有这个错误,一直说我的一个字符串使用共享首选项为空

时间:2016-02-25 08:06:34

标签: java android sharedpreferences

MainMenu类不断在一个声明为null的String上抛出一个错误,而我最难解决这个问题。

它抛出了这个MainMenu类的NameText.setText()。

如果这很容易解决,我会感到愚蠢lol

MainMenu.class:

package com.example.basicrecipes;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainMenu extends Activity implements OnClickListener {

    public static final String RecipeNamesPref = "RecipeNames";
    public static final String NamePref = "Name";
    public static final String DescriptionPref = "Description";
    public static final String PrefSteps = "How to Prepare";
    public static final String CuisinePref = "American";
    public static String SelectedRecipe = "SelectedRecipe";


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

        View aboutButton = findViewById(R.id.main_about_button);
        aboutButton.setOnClickListener(this);

        Button newRecipe = (Button) this.findViewById(R.id.main_new_button);
        newRecipe.setOnClickListener(this);

        Button exitApp = (Button) this.findViewById(R.id.main_exit_button);
        exitApp.setOnClickListener(this);

        Button listRecipe = (Button) this.findViewById(R.id.main_list_button);
        listRecipe.setOnClickListener(this);

        SharedPreferences selectedRecipe;
        selectedRecipe = getSharedPreferences(MainMenu.SelectedRecipe, RecipeEntry.MODE_PRIVATE);
        SelectedRecipe = selectedRecipe.getString(MainMenu.SelectedRecipe, "New Recipe");

        if ("New Recipe" != SelectedRecipe) {
            SharedPreferences thisRecipe = getSharedPreferences(SelectedRecipe + "_Detail", RecipeEntry.MODE_PRIVATE);
            EditText NameText = (EditText) this.findViewById(R.id.name_new);
            NameText.setText(thisRecipe.getString(MainMenu.NamePref, ""));
            EditText DescriptionText = (EditText) this.findViewById(R.id.description_new);
            DescriptionText.setText(thisRecipe.getString(MainMenu.DescriptionPref, ""));
            EditText TextSteps = (EditText) this.findViewById(R.id.new_steps);
            TextSteps.setText(thisRecipe.getString(MainMenu.PrefSteps, ""));
            Spinner CuisineSelect = (Spinner) this.findViewById(R.id.cuisine_new);
            CuisineSelect.setSelection(thisRecipe.getInt(MainMenu.CuisinePref, 0));
        }

    }

    @Override
    public void onClick(View thisView) {
        switch (thisView.getId()) {
        case R.id.main_about_button:
            Intent showAbout = new Intent(this, About.class);
            startActivity(showAbout);
            break;
        case R.id.main_list_button:
            Intent doMenuClick = new Intent(this, RecipeList.class);
            startActivity(doMenuClick);
            break;
        case R.id.main_new_button:
            doMenuClick = new Intent(this, RecipeNew.class);
            startActivity(doMenuClick);
            break;
        case R.id.main_exit_button:
            moveTaskToBack(true);
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inf = getMenuInflater();
        inf.inflate(R.menu.menu, menu);
        // menu.findItem(R.id.main_menu_search).setIntent(new Intent(this, SearchRecipe.class));
        menu.findItem(R.id.main_menu_options).setIntent(new Intent(this, Options.class));
        menu.findItem(R.id.main_menu_new).setIntent(new Intent(this, RecipeEntry.class));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem itm) {
        super.onOptionsItemSelected(itm);
        Intent menuIntent = itm.getIntent();
        if (menuIntent != null)
            startActivity(menuIntent);
        return true;
    }
}

3 个答案:

答案 0 :(得分:0)

问题在于:

if ("New Recipe" != SelectedRecipe) {

比较这样的字符串只是比较Object类型,而不是字符串值本身。

我猜测SharedPreferences SelectedRecipe_Detail 不存在。

因此,当你尝试getString()时,会抛出NPE。

解决方案:

按照以下方式进行检查:

if(!"New Recipe".equals(SelectedRecipe)) {

答案 1 :(得分:0)

发现问题 您正在使用SelectedRecipe作为常量

public static String SelectedRecipe = "SelectedRecipe";

然后您将在

中更新其值
   SelectedRecipe = selectedRecipe.getString(MainMenu.SelectedRecipe, "New Recipe");

然后你再次使用它,这是错误的

    SharedPreferences thisRecipe = getSharedPreferences(SelectedRecipe + "_Detail", RecipeEntry.MODE_PRIVATE);

更改以下代码

       String SelectedRecipe = selectedRecipe.getString(MainMenu.SelectedRecipe, "New Recipe");

        if ("New Recipe" != SelectedRecipe) {

to 

        String retSelectedRecipe = selectedRecipe.getString(MainMenu.SelectedRecipe, "New Recipe");

        if (!"New Recipe".equals(retSelectedRecipe) ) {

答案 2 :(得分:0)

我想我知道问题所在。

我得到了这本书: The Complete Idiots Guide to Android App Development

这是在2011年发布的,我很确定自那时起事情发生了变化。 我建议你不要去买这本书!警告!!! 这本书非常糟糕!!!

在整本书中,变量发生了变化,事情发生了变化,而不仅仅是这样,他没有详细说明任何重要细节。这是非常半!!!!