0

我所拥有的给了我这些错误。

04-13 15:01:41.600: E/AndroidRuntime(8431): FATAL EXCEPTION: main
04-13 15:01:41.600: E/AndroidRuntime(8431): java.lang.RuntimeException: Unable to start activity ComponentInfo    {com.omnilabs.alexandernapoles.hl2sticl/com.example.example.example.MainClass}: java.lang.NullPointerException
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.os.Looper.loop(Looper.java:137)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at dalvik.system.NativeStart.main(Native Method)
04-13 15:01:41.600: E/AndroidRuntime(8431): Caused by: java.lang.NullPointerException
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.preference.PreferenceManager.getDefaultSharedPreferencesName    (PreferenceManager.java:371)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.preference.PreferenceManager.getDefaultSharedPreferences    (PreferenceManager.java:366)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at com.example.example.example.ShakeEventListener.<init>(ShakeEventListener.java:24)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at com.example.example.example.MainClass.onCreate(StunBatton.java:38)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.Activity.performCreate(Activity.java:4465)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-13 15:01:41.600: E/AndroidRuntime(8431):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-13 15:01:41.600: E/AndroidRuntime(8431):     ... 11 more

这是我的shakevent监听器类的一部分,涉及偏好

public class ShakeEventListener extends Application implements SensorEventListener {
public void onCreate(Bundle savedInstanceState) {

    super.onCreate();


}
SharedPreferences preferences =
         PreferenceManager.getDefaultSharedPreferences(this);


private String editTextPref = getString(R.string.preference_key);
int nVar = preferences.getInt(editTextPref, MODE_PRIVATE);
int MIN_FORCE = nVar;
/**
* Minimum times in a shake gesture that the direction of movement needs to
* change.
*/
private static final int MIN_DIRECTION_CHANGE = 15;

/** Maximum pause between movements. */
private static final int MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE = 50000;

等等等等

首选项.java

package com.example.example.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Preferences extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.layout.preferences);


        }
}

首选项.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
                android:title="First Category">
        <EditTextPreference
                android:name="Stun Swing Sensitivity"
                android:summary="This allows you to custom fit your sensitivity."
                android:defaultValue="10"
               android:numeric="integer"
                android:maxLength="200"
                android:title="Sensitivity Level"
                android:key="@string/preference_key" />
        </PreferenceCategory>

</PreferenceScreen>

在strings.xml里面我把editTextPref

所以基本上我只想知道如何在一个活动要求摇动的类文件中使用一个变量,然后它使用 onShake(); 从摇动活动中调用,但我想将 MIN_FORCE 保存为变量 nVar 并且 nVar 是首选项文件中的设置,即整数。

请帮忙!!!!!!!!!

编辑:我知道这与这段代码有关

      SharedPreferences preferences =
             PreferenceManager.getDefaultSharedPreferences(this);
 String editTextPref = getString(R.string.preference_key);
nVar = preferences.getInt(editTextPref, 10);
4

1 回答 1

1

您必须将呼叫移至getDefaultSharedPreferences内部onCreate(或稍后的回调)。它在外部不起作用,因为在对象构建时您的 Activity 没有完全设置。

public class ShakeEventListener extends Application implements SensorEventListener {

int nVar = 0;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate();
    SharedPreferences preferences =
         PreferenceManager.getDefaultSharedPreferences(this);


    private String editTextPref = getString(R.string.preference_key);
    nVar = preferences.getInt(editTextPref, MODE_PRIVATE);
    int MIN_FORCE = nVar;
}
于 2012-04-13T19:43:35.037 回答