0
單擊調用getdeets的按鈕後,我的應用程序崩潰。我不知道爲什麼。將字符串顯示在EditText中崩潰應用程序
我希望數據被添加,然後在按鈕單擊時保存,然後數據保留在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
你會發現,在頂部宣佈TextViews。我試圖將這些設置爲抓取的數據,但它也使應用程序崩潰。 這裏的活動代碼:
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);
}
}
線ArmorStatsSP.java:65是什麼? – 2014-09-01 09:13:38
你在哪裏初始化了sharedPreference「pref」? – user1728071 2014-09-01 09:16:13
這就是爲什麼它顯示空指針。 – user1728071 2014-09-01 09:16:28