1

我现在已经获得了所有代码,但一直在崩溃。基本上,代码是一个按钮,单击该按钮时,会在金额中添加一个数字。因此,单击一次时,它将为 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>
4

4 回答 4

2

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

此外,您应该调用edit()每个onResume(); 否则,您可能会在其中获得旧数据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();
    }
}
于 2013-02-10T18:05:19.100 回答
0

始终检查您的变量 ( settings, editor) 是否不为空...

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

@Override
public void onResume() {
   super.onResume();  // Always call the superclass method first
   SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
  editor.putInt("mCount", mCount);
 editor.commit();
 }
于 2013-02-10T18:01:45.717 回答
0

尝试将 getSharedPreferences 移动到 Activity.onCreate 我认为在此之前无法完成。

// 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;
private SharedPreferences.Editor editor; 

@Override
public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    editor = settings.edit();
}
于 2013-02-10T18:04:24.307 回答
0

看这里

这样做。我希望它会帮助你。

 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.layout);
   ..some code.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
   Editor editor = settings.edit();
 }
于 2013-02-10T18:04:45.937 回答