应用程序继续崩溃

时间:2013-02-10 17:56:43

标签: java android

我现在已经拥有了所有代码,但一直在崩溃。基本上,代码是一个按钮,单击该按钮时,会向一个数量添加一个数字。所以,点击一次,它将是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 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:0)

始终检查您的变量(settingseditor)是否为空...

@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();
 }

答案 2 :(得分: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();
}

答案 3 :(得分: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();
 }