问题描述
我已经给了我这些错误。
15 04-13:01:41.600:E / AndroidRuntime(8431):致命异常:主要
04-13 15:01:41.600:E / AndroidRuntime(8431):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.omnilabs.alexandernapoles.hl2sticl / com.example.example.example.MainClass}的java.lang 。空指针异常
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.os.Handler.dispatchMessage(Handler.java:99)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.os.Looper.loop(Looper.java:137)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 15:01:41.600:E / AndroidRuntime(8431):在java.lang.reflect.Method.invokeNative(本机方法)
04-13 15:01:41.600:E / AndroidRuntime(8431):在java.lang.reflect.Method.invoke(Method.java:511)
04-13 15:01:41.600:E / AndroidRuntime(8431):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:787)
04-13 15:01:41.600:E / AndroidRuntime(8431):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-13 15:01:41.600:E / AndroidRuntime(8431):在dalvik.system.NativeStart.main(本机方法)
04-13 15:01:41.600:E / AndroidRuntime(8431):因:显示java.lang.NullPointerException
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
04-13 15:01:41.600:E / AndroidRuntime(8431):在Android的preference preferenceManager.getDefaultShared preferencesName(preferenceManager.java:371)。
04-13 15:01:41.600:E / AndroidRuntime(8431):在Android的preference preferenceManager.getDefaultShared preferences(preferenceManager.java:366)。
04-13 15:01:41.600:E / AndroidRuntime(8431):在com.example.example.example.ShakeEventListener<&初始化GT;(ShakeEventListener.java:24)
04-13 15:01:41.600:E / AndroidRuntime(8431):在com.example.example.example.MainClass.onCreate(StunBatton.java:38)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.Activity.performCreate(Activity.java:4465)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-13 15:01:41.600:E / AndroidRuntime(8431):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-13 15:01:41.600:E / AndroidRuntime(8431):11 ...更多
这是我的听众shakevent类的一部分,涉及到preferences
公共类ShakeEventListener扩展应用实现SensorEventListener {
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate();
}
共享preferences preferences =
preferenceManager.getDefaultShared preferences(本);
私人字符串EDITTEXT preF =的getString(R.string preference_key);
INT NVAR = preferences.getInt(EDITTEXT preF,MODE_PRIVATE);
INT MIN_FORCE = NVAR;
/ **
*在摇姿态最小倍,运动方向需要
*改变。
* /
私有静态最终诠释MIN_DIRECTION_CHANGE = 15;/ **运动之间最大暂停。 * /
私有静态最终诠释MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE = 50000;
等等等等
preferences.java
包com.example.example.example;进口android.os.Bundle;
。进口的Android preference preferenceActivity;公共类preferences扩展$ P $ {pferenceActivity
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
加preferencesFromResource(R.layout preferences);
}
}
preferences.xml
<?XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android>
< preferenceCategory
机器人:标题=第一类>
< EditText上preference
机器人:名字=眩晕摇摆灵敏度
机器人:汇总=这可以让你定制适合您的敏感性。
机器人:设置defaultValue =10
机器人:数字=整数
机器人:最大长度=200
机器人:标题=敏感度等级
机器人:键=@字符串/ preference_key/>
< / preferenceCategory>< / preferenceScreen>
strings.xml中的内幕我把
EDITTEXT preF
所以基本上我只是想知道我怎么能在一个活动呼吁晃动类文件中使用的变量则使用onShake();从摇活性打电话,但我想MIN_FORCE保存为一个变量NVAR和NVAR是在preference文件设置那一个整数。
请帮助!!!!!!!!!
编辑:
我知道它有这个code做
共享preferences preferences =
preferenceManager.getDefaultShared preferences(本);
字符串EDITTEXT preF =的getString(R.string preference_key);
NVAR = preferences.getInt(EDITTEXT preF,10);
您必须将呼叫转移到 getDefaultShared preferences
在的onCreate
(或更高回调)。它不会因为你的活动是没有完全建立对象施工时间以外工作。
公共类ShakeEventListener扩展应用实现SensorEventListener {INT NVAR = 0;
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate();
共享preferences preferences =
preferenceManager.getDefaultShared preferences(本);
私人字符串EDITTEXT preF =的getString(R.string preference_key);
NVAR = preferences.getInt(EDITTEXT preF,MODE_PRIVATE);
INT MIN_FORCE = NVAR;
}
What I have Gives Me these errors.
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
And here is part of my shakevent listener class that involves preferences
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;
etc etc
preferences.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);
}
}
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>
Inside of strings.xml i put editTextPref
So Basically I just want to know how I can use a variable in a class file that an activity calls for shaking then it uses onShake(); to call from the shake activity, But I want to save MIN_FORCE as a variable nVar and nVar be the setting in the preference files thats an integer.
Please Help!!!!!!!!!
EDIT:I know it has to do with this code
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);
String editTextPref = getString(R.string.preference_key);
nVar = preferences.getInt(editTextPref, 10);
You have to move the call to getDefaultSharedPreferences
inside onCreate
(or a later callback). It does not work outside because at object construction time your Activity isn't fully set up.
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;
}
这篇关于我如何使用preference活动来存储一个shakeeventlistener类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!