2014-04-30 93 views
-3

我正在开发一个android应用程序,用户被告知要绘制什么字母并使用手势处理字母识别。我不知道为什么这会突破。在eclipse中没有检测到错误,但logcat说它是一个空指针异常。这里是我的应用程序代码:无法解释的空指针错误

public class MainActivity extends Activity implements OnGesturePerformedListener 
{ 
    GestureLibrary mLibrary; 
    Resources res; 
    String [] letters; 
    TextView tv = (TextView) findViewById(R.id.text); 
    int i = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); 
     if (!mLibrary.load()) 
     { 
      finish(); 
     } 

     res = getResources(); 
     letters = res.getStringArray(R.array.LetterToBeDrawn); 
     tv.setText("Draw the letter: " + letters[i]); 

     GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestures); 
     gestures.addOnGesturePerformedListener(this); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) 
    { 
     ArrayList<Prediction> predictions = new ArrayList<Prediction>(); //this methods asks to recognize the gesture against loaded gesture library 
     predictions = mLibrary.recognize(gesture); 

     // We want at least one prediction 
     if (predictions.size() > 0) 
     { 
      Prediction prediction = predictions.get(0); //get the 1st prediction auto generated for you by Android 
      // We want at least some confidence in the result 
      if (prediction.score > 1.0 && prediction.name.equals(letters[i])) 
      { 
       // Show the spell 
       Toast.makeText(MainActivity.this, prediction.name, Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
} 

我的XML:

<android.gesture.GestureOverlayView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/gestures" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:fadeOffset="1000" 
    android:gestureStrokeType="multiple" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

</android.gesture.GestureOverlayView> 

的logcat的:

04-30 14:36:45.716: D/AndroidRuntime(1558): Shutting down VM 
04-30 14:36:45.716: W/dalvikvm(1558): threadid=1: thread exiting with uncaught exception (group=0xb2a27ba8) 
04-30 14:36:45.776: E/AndroidRuntime(1558): FATAL EXCEPTION: main 
04-30 14:36:45.776: E/AndroidRuntime(1558): Process: com.gmail.Sheridjohn.letterchecker, PID: 1558 
04-30 14:36:45.776: E/AndroidRuntime(1558): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gmail.Sheridjohn.letterchecker/com.gmail.Sheridjohn.letterchecker.MainActivity}: java.lang.NullPointerException 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.os.Handler.dispatchMessage(Handler.java:102) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.os.Looper.loop(Looper.java:136) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at java.lang.reflect.Method.invoke(Method.java:515) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at dalvik.system.NativeStart.main(Native Method) 
04-30 14:36:45.776: E/AndroidRuntime(1558): Caused by: java.lang.NullPointerException 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.Activity.findViewById(Activity.java:1884) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at com.gmail.Sheridjohn.letterchecker.MainActivity.<init>(MainActivity.java:23) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at java.lang.Class.newInstanceImpl(Native Method) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at java.lang.Class.newInstance(Class.java:1208) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112) 
04-30 14:36:45.776: E/AndroidRuntime(1558):  ... 11 more 

回答

0

你的活动呼吁findViewById之前setContentView被调用。将tv = (TextView) findViewById(R.id.text);移至onCreate。

2

您的布局设置为活动前有

TextView tv = (TextView) findViewById(R.id.text); 

。您的初始化失败tv为空。您需要首先将布局设置为活动,然后初始化视图。

而当你有

tv.setText("Draw the letter: " + letters[i]); 

您所呼叫的setText当电视实际上是空

http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/app/Activity.java#1884

发布由laalto提供的链接的源。多亏了他,我纠正我的职务

1877 /** 
1878  * Finds a view that was identified by the id attribute from the XML that 
1879  * was processed in {@link #onCreate}. 
1880  * 
1881  * @return The view if found or null otherwise. 
1882  */ 
1883 public View findViewById(int id) { 
1884  return getWindow().findViewById(id); 
1885 } 
1886 

你的堆栈跟踪不表明原因它不是不明原因正如你在标题

发表致:04-30显示java.lang.NullPointerException 14:36:45.776 : E/AndroidRuntime(1558):at android.app.Activity.findViewById(Activity.java:1884)04-30 14:36:45.776:E/AndroidRuntime(1558):at com.gmail.Sheridjohn。 letterchecker.MainActivity。(MainActivity.java:23)

+0

谢谢,我原来就是这样,把它改回原处,但它仍然没有工作,我得到了logcat结果。 – user2316046

+0

@ user2316046它会工作的崩溃可能是因为不同的原因,如果你更新的建议 – Raghunandan

2

原因是你在初始化成员变量时在onCreate()之前调用findViewById()。您的活动没有Window尚需findViewById()需要。

仅供参考,以下是导致NPE的代码行的平台源代码:http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/app/Activity.java#1884-getWindow()返回null那里。

setContentView()由其他答案给出的解释有效,但并不完全正确。如果你有Window,并且在setContentView()之前有callind findViewById(),则会返回null而不是抛出NPE。

解决方法仍然相同:在setContentView()之后将初始化移动到onCreate()

+0

我得到它的电视为空,然后用电视来设置文本导致NPE。谢谢你要更新 – Raghunandan

+0

@Raghunandan在堆栈中跟踪NPE,因为'getWindow()'返回null。 http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/app/Activity.java#1884 – laalto

+0

表示同意。我刚纠正了我的帖子 – Raghunandan