2013-02-10 82 views
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

您想发布您的LogCat堆栈跟踪吗?没有这个,我们只能猜测。 – 2013-02-10 17:57:58

回答

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

仍然与该更新崩溃 – James 2013-02-10 18:04:20

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

你应该在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(); 
    } 
} 
+1

好吧刚刚添加,它仍然崩溃 – James 2013-02-10 18:10:35

+0

@詹姆斯我已经编辑了上面的代码;现在就试试。如果它仍然无效,请使用更新的代码和LogCat更新您的初始文章。 – Eric 2013-02-10 18:12:45

+1

好吧,我的代码已更新到现在正在使用的代码中,但现在它的稳定但是没有按钮可以点击成为代码并且没有文本框ive也将xml置于更新的版本中 – James 2013-02-10 18:38:16