为活动分配视图时是否为空指针异常?

时间:2010-07-27 14:01:04

标签: android android-activity nullpointerexception

我已经构建了一个活动来处理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中看到的,一个名为gestures的视图将用于此目的,但在尝试给视图充气时,我一直收到通胀错误。在我遇到这种情况之前,它是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>

1 个答案:

答案 0 :(得分:2)

findViewById()要求使用setContentView()或在视图上调用。 所以我建议你,或者叫做setContentView(R.layout.nameOfYourXmlfile),或者用Layout inflater给它充气。