我已经构建了一个 Activity 来处理我的 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)
我知道我需要为 Activity 分配一个 View ,正如您将在我的 XML 中看到的那样,一个名为 gestures 的 View 将用于此目的,但我在尝试膨胀 View 时不断收到 Inflation 错误。之前发生在我身上的是 XML 中 View 名称的拼写错误,但这次我不这么认为。
我如何正确地将“手势” View 分配给此 Activity 以防止错误?
我希望有人知道答案并能帮助我!!非常感谢
我在我的代码中这样定义了我的 Activity :
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() 方法中创建了 Activity ,方法是:
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>
最佳答案
findViewById()
需要使用 setContentView()
或在 View 上调用。
所以我建议您调用 setContentView(R.layout.nameOfYourXmlfile) 或使用 Layout inflater 对其进行充气。
关于android - 将 View 分配给 Activity 时出现空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3344401/