私は、ユーザーが getText に数値を入力し、ボタンをクリックしてユーザーの入力を以前の結果に追加し、これを画面に表示するという最初のアプリの 1 つを作成しています。データを SharedPreferences に保存しようとしましたが、ユーザーがホーム ボタンを押してからアプリに戻ると機能しますが、戻るボタンを押してからアプリに戻るとアプリがクラッシュします。何かアドバイス?
public class Calc extends Activity {
int points = 0;
SharedPreferences data;
public static String filename = "Points";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data = getSharedPreferences(filename, 0);
points = data.getInt("key", 0);
final TextView myTextView = (TextView) findViewById(R.id.mytextview);
myTextView.setText("" + points);
Button Button = (Button) findViewById(R.id.add);
final EditText Edit = (EditText) findViewById(R.id.input);
Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = 0;
try {
temp = Integer.parseInt(Edit.getText().toString());
} catch (NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
points = points + temp;
if (points > 9999) {
myTextView.setText("10000+");
} else {
myTextView.setText("" + points);
}
Edit.setText("");
SharedPreferences.Editor editor = data.edit();
editor.putInt("key", points);
editor.commit();
}
});
}
}
クラッシュログ:
05-19 03:24:02.352: D/dalvikvm(1120): GC_EXTERNAL_ALLOC freed 759 objects / 55808 bytes in 63ms
05-19 03:24:05.812: D/dalvikvm(1120): GC_EXTERNAL_ALLOC freed 193 objects / 9224 bytes in 50ms
05-19 03:24:08.842: W/KeyCharacterMap(1120): No keyboard for id 0
05-19 03:24:08.842: W/KeyCharacterMap(1120): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-19 03:24:11.052: W/IInputConnectionWrapper(1120): showStatusIcon on inactive InputConnection
05-19 03:24:14.952: E/dalvikvm-heap(1120): 16704000-byte external allocation too large for this process.
05-19 03:24:14.999: E/GraphicsJNI(1120): VM won't let us allocate 16704000 bytes
05-19 03:24:15.022: D/AndroidRuntime(1120): Shutting down VM
05-19 03:24:15.061: W/dalvikvm(1120): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-19 03:24:15.223: E/AndroidRuntime(1120): FATAL EXCEPTION: main
05-19 03:24:15.223: E/AndroidRuntime(1120): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Calc}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.os.Looper.loop(Looper.java:123)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-19 03:24:15.223: E/AndroidRuntime(1120): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 03:24:15.223: E/AndroidRuntime(1120): at java.lang.reflect.Method.invoke(Method.java:521)
05-19 03:24:15.223: E/AndroidRuntime(1120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-19 03:24:15.223: E/AndroidRuntime(1120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-19 03:24:15.223: E/AndroidRuntime(1120): at dalvik.system.NativeStart.main(Native Method)
05-19 03:24:15.223: E/AndroidRuntime(1120): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
05-19 03:24:15.223: E/AndroidRuntime(1120): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-19 03:24:15.223: E/AndroidRuntime(1120): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.Activity.setContentView(Activity.java:1647)
05-19 03:24:15.223: E/AndroidRuntime(1120): at com.example.Calc.onCreate(Calc.java:32)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-19 03:24:15.223: E/AndroidRuntime(1120): ... 11 more
05-19 03:24:15.223: E/AndroidRuntime(1120): Caused by: java.lang.reflect.InvocationTargetException
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.widget.FrameLayout.<init>(FrameLayout.java:79)
05-19 03:24:15.223: E/AndroidRuntime(1120): at java.lang.reflect.Constructor.constructNative(Native Method)
05-19 03:24:15.223: E/AndroidRuntime(1120): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
05-19 03:24:15.223: E/AndroidRuntime(1120): ... 21 more
05-19 03:24:15.223: E/AndroidRuntime(1120): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.Bitmap.nativeCreate(Native Method)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.content.res.Resources.loadDrawable(Resources.java:1709)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.View.<init>(View.java:1885)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.view.ViewGroup.<init>(ViewGroup.java:291)
05-19 03:24:15.223: E/AndroidRuntime(1120): at android.widget.FrameLayout.<init>(FrameLayout.java:83)
05-19 03:24:15.223: E/AndroidRuntime(1120): ... 25 more
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >
<ImageView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_gravity="center_horizontal|top"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:src="@drawable/title" />
<EditText
android:id="@+id/input"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="80dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/add"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dp"
android:text="Add Points" />
<TextView
android:id="@+id/youHave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="100dp"
android:background="#AA968454"
android:padding="10dp"
android:text="You have"
android:textColor="#ffffffff" />
<TextView
android:id="@+id/mytextview"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="150dp"
android:background="#AA968454"
android:gravity="center"
android:textColor="#ffffffff"
android:textSize="60dp" />
</FrameLayout>