获取要在EditText中显示的字符串会导致应用崩溃

时间:2014-09-01 09:12:47

标签: android android-edittext textview sharedpreferences

单击调用getdeets的按钮后,我的应用程序崩溃了。我不知道为什么。

我希望添加数据,然后在按钮单击时保存,然后将数据保留在EditText中永久,直到它在同一EditText中更改为不同的值。那么这个新价值将永远存在。

logcat的:

    09-01 04:59:06.890: W/dalvikvm(1710): threadid=1: thread exiting with uncaught exception (group=0x41465700)
    09-01 04:59:06.930: E/AndroidRuntime(1710): FATAL EXCEPTION: main
    09-01 04:59:06.930: E/AndroidRuntime(1710): java.lang.IllegalStateException: Could not execute method of the activity
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.view.View$1.onClick(View.java:3633)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.view.View.performClick(View.java:4240)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.view.View$PerformClick.run(View.java:17721)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.os.Handler.handleCallback(Handler.java:730)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.os.Handler.dispatchMessage(Handler.java:92)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.os.Looper.loop(Looper.java:137)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invoke(Method.java:525)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at dalvik.system.NativeStart.main(Native Method)
    09-01 04:59:06.930: E/AndroidRuntime(1710): Caused by: java.lang.reflect.InvocationTargetException
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invoke(Method.java:525)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at android.view.View$1.onClick(View.java:3628)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     ... 11 more
    09-01 04:59:06.930: E/AndroidRuntime(1710): Caused by: java.lang.NullPointerException
    09-01 04:59:06.930: E/AndroidRuntime(1710):     at com.mikitz.rogsimple.ArmorStatsSP.getdeets(ArmorStatsSP.java:65)
    09-01 04:59:06.930: E/AndroidRuntime(1710):     ... 14 more

您会注意到在顶部声明的TextView。我试图将这些设置为获取的数据,但它也会崩溃应用程序。 这是活动的代码:

    package com.mikitz.rogsimple;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class ArmorStatsSP extends Activity {

EditText armorhealthchest, healthchest, abvchest;
TextView ChestAH, ChestH, ChestABV;
EditText armorhealthhead, healthheaad, abvhead;
EditText armorhealthrightarm, healthrightarm, abvrightarm; 
EditText armorhealthleftarm, healthleftarm, abvleftarm; 
EditText armorhealthrightleg, healthrightleg, abvrightleg; 
EditText armorhealthleftleg, healthleftleg, abvleftleg;
SharedPreferences pref;
String getAHChest, getHChest, getABVChest;

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

    armorhealthchest = (EditText) findViewById(R.id.armorhealthchest);
    healthchest = (EditText) findViewById(R.id.healthchest);
    abvchest = (EditText) findViewById(R.id.abvchest);
}       

public void onBackPressed() 
{
    SharedPreferences sharedPreferences = getSharedPreferences("Armor_Stats", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=sharedPreferences.edit();           
            editor.putString("armorhealthchest", armorhealthchest.getText().toString());
            editor.putString("healthchest", healthchest.getText().toString());
            editor.putString("abvchest", abvchest.getText().toString());
            editor.commit();

    Toast.makeText(this, "GREAT SUCCESS!!!!", Toast.LENGTH_LONG).show();

    Intent intent = new Intent (this, MainActivity.class);
        startActivity(intent);

}

public void save (View view)
{
    SharedPreferences sharedPreferences = getSharedPreferences("Armor_Stats", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=sharedPreferences.edit();           
            editor.putString("armorhealthchest", armorhealthchest.getText().toString());
            editor.putString("healthchest", healthchest.getText().toString());
            editor.putString("abvchest", abvchest.getText().toString());
            editor.commit();

    Toast.makeText(this, "GREAT SUCCESS!!!!", Toast.LENGTH_LONG).show();
}

public void getdeets (View view)
{
    getAHChest = pref.getString("armorhealthchest", "");
    getHChest = pref.getString("healthchest", "");
    getABVChest = pref.getString("abvchest", "");
    armorhealthchest.setText(getAHChest);
    healthchest.setText(getHChest);
    abvchest.setText(getABVChest);

}

}

1 个答案:

答案 0 :(得分:0)

似乎在getdeets方法pref中为null,因此在onCreate方法中将其初始化为

pref = getSharedPreferences("Armor_Stats", Context.MODE_PRIVATE);