gpt4 book ai didi

java - 应用程序不断崩溃

转载 作者:行者123 更新时间:2023-11-29 21:49:33 24 4
gpt4 key购买 nike

我现在已经得到了所有的代码,但总是崩溃。基本上,该代码是一个按钮,单击该按钮会将一个数字添加到一个金额中。所以,点击一次,就是1;再次点击将变成 2,然后是 3,依此类推。

package com.example.counter;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;


public class MainActivity extends Activity {
// Private member field to keep track of the count
private int mCount = 0;

public static final String PREFS_NAME = "com.example.myApp.mCount";
private SharedPreferences settings = null;
private SharedPreferences.Editor editor = null;

/** ADD THIS METHOD **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass method first
settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}

@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
mCount = settings.getInt("mCount", 0);
}

@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
editor = settings.edit(); /** ADD THIS LINE **/
editor.putInt("mCount", mCount);
editor.commit();
}
}

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/TextViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />

<Button
android:id="@+id/ButtonCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/TextViewCount"
android:layout_alignRight="@+id/TextViewCount"
android:layout_marginBottom="22dp"
android:text="Count" />

</RelativeLayout>

最佳答案

您应该在 onCreate() 中创建 SharedPreferences 对象;在此之前,它不能保证被创建。

此外,您应该在每个 onResume() 中调用 edit();否则,您可能会在该 Editor 中获得旧数据。

public class MainActivity extends Activity {
// Private member field to keep track of the count
private int mCount = 0;

public static final String PREFS_NAME = "com.example.myApp.mCount";
private SharedPreferences settings = null;
private SharedPreferences.Editor editor = null;

/** ADD THIS METHOD **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass method first
settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}

@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
mCount = settings.getInt("mCount", 0);
}

@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
editor = settings.edit(); /** ADD THIS LINE **/
editor.putInt("mCount", mCount);
editor.commit();
}
}

关于java - 应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14800775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com