2
我已經建立了一個活動來處理我的Android遊戲的手勢,並希望它的手勢在屏幕上任何迴應,但我得到的日誌此錯誤:將視圖分配給活動時的空指針異常?
07-27 13:55:07.268: ERROR/AndroidRuntime(751): java.lang.NullPointerException
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at android.app.Activity.findViewById(Activity.java:1637)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView$DistractionsThread$GestureActivity.onCreate(DistractionsView.java:282)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView$DistractionsThread.doStart(DistractionsView.java:559)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView$DistractionsThread.doKeyDown(DistractionsView.java:829)
07-27 13:55:07.268: ERROR/AndroidRuntime(751): at com.darius.android.distractions.DistractionsView.onKeyDown(DistractionsView.java:1278)
我明白我需要分配查看活動,並且您將在我的XML中看到,名爲手勢的視圖將用於此目的,但在嘗試膨脹視圖時,我會收到膨脹錯誤。在發生這種情況之前,這是XML中視圖名稱的拼寫錯誤,但這不是我想的這次。
我將如何正確地分配了「手勢」爲了這次活動,以防止錯誤?
我不希望有人知道答案,可以幫助我!說
public class GestureActivity extends Activity implements GestureOverlayView.OnGesturePerformedListener{
public void onCreate(Bundle savedInstanceState){
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
Log.w(getClass().getName(), "GESTURE ACTIVITY CREATED");
// Load the gesture library
Log.w(getClass().getName(), "LOADING GESTURE LIBRARY");
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if(!mLibrary.load()){
finish();
}
/*
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
Log.w(getClass().getName(), "GESTURE ACTIVITY CREATED");
*/
}
public void onStart(){
//I replicated the code here from onCreate but will change this!!
GestureOverlayView gestures = (GestureOverlayView) findViewById (R.id.gestures);
gestures.addOnGesturePerformedListener(this);
Log.w(getClass().getName(), "GESTURE ACTIVITY CREATED");
// Load the gesture library
Log.w(getClass().getName(), "LOADING GESTURE LIBRARY");
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if(!mLibrary.load()){
finish();
}
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture){ ...}
}
我已經建立在主線程在它的DOSTART()方法的活動:
GestureActivity mGestureActivity = new GestureActivity();
mGestureActivity.onCreate(null);
和許多感謝
我已經定義我的活動我像這樣的代碼我的XML佈局看起來像這樣:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.darius.android.distractions.DistractionsView
android:id="@+id/distractions_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:text="Hello"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#88ffffff"
android:textSize="24sp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/concentration_bar"
android:textColor = "#EB0000"
android:textSize="10sp"
android:id="@+id/conbartext"
android:visibility="visible"
></TextView>
</RelativeLayout>
謝謝你,所以如果我將其更改爲: \t \t \t \t \t \t公共無效的onCreate(捆綁savedInstanceState){ \t \t \t \t的setContentView(R.layout.main); \t 我該如何去附加我以前使用過這個代碼的監聽器: GestureOverlayView gestures =(GestureOverlayView)findViewById(R.id.gestures); \t \t \t \t \t \t gestures.addOnGesturePerformedListener(this); – ComethTheNerd 2010-07-27 14:41:47
你應該能夠做到這一點就像這樣,作爲所謂的ID存在於你在XML'setContentView' – Sephy 2010-07-27 14:51:19
是的,我希望的工作,但我已經注意到這條線不斷彈出時,它建立並果然NullPointerException異常如下當我嘗試與程序的交互: 07-27 14:58:06.048:WARN/InputManagerService(65):對非重點客戶com.android.internal.view.IInputMethodClient $存根$代理開始輸入@ 43ea09c0(uid = 10000 pid = 122) – ComethTheNerd 2010-07-27 15:00:40