2014-04-13 19 views
0

我剛開始學習Android編程,並遇到麻煩。我正在使用「Android編程大書呆子牧場指南」一書。我的IDE是Eclipse和Genymotion來模擬應用程序。這裏的logcat的:第一個程序導致大量錯誤

04-13 00:19:48.065: D/dalvikvm(1256): Late-enabling CheckJNI 
04-13 00:19:48.781: D/AndroidRuntime(1256): Shutting down VM 
04-13 00:19:48.785: W/dalvikvm(1256): threadid=1: thread exiting with uncaught exception (group=0xa4d8ab20) 
04-13 00:19:48.785: E/AndroidRuntime(1256): FATAL EXCEPTION: main 
04-13 00:19:48.785: E/AndroidRuntime(1256): Process: com.bignerdranch.android.geoquiz, PID: 1256 
04-13 00:19:48.785: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.os.Handler.dispatchMessage(Handler.java:102) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.os.Looper.loop(Looper.java:136) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at java.lang.reflect.Method.invoke(Method.java:515) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at dalvik.system.NativeStart.main(Native Method) 
04-13 00:19:48.785: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:30) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.Activity.performCreate(Activity.java:5231) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
04-13 00:19:48.785: E/AndroidRuntime(1256):  ... 11 more 
04-13 00:19:56.345: I/Process(1256): Sending signal. PID: 1256 SIG: 9 

這裏的fragment_quiz.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="24dp" 
     android:text="@string/question_text" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/true_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/true_button" /> 

     <Button 
      android:id="@+id/false_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/false_button" /> 

    </LinearLayout> 

</LinearLayout> 

的QuizActivity.java文件(由大部分進口的Eclipse代碼):

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Toast; 

public class QuizActivity extends ActionBarActivity { 

    private Button mTrueButton; 
    private Button mFalseButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_quiz); 

     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 

     mTrueButton = (Button) findViewById(R.id.true_button); 
     mTrueButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT) 
         .show(); 

      } 
     }); 

     mFalseButton = (Button) findViewById(R.id.false_button); 
     mFalseButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT) 
         .show(); 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.quiz, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_quiz, container, 
        false); 
      return rootView; 
     } 
    } 

} 

我在做什麼錯誤?

+1

引起:java.lang.NullPointerException 04-13 00:19:48.785:E/AndroidRuntime(1256):at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:30)看看line 30並找到具有空值的變量,然後在代碼中回溯以查看其爲空的原因。 – NormR

+0

mTrueButton是否爲空?因爲我通過ID將它分配給按鈕,所以我看不出來。 –

回答

1

將您的佈局定義放置在PlaceholderFragment類中。對於findViewById,請使用rootView.findViewById。在上下文中,使用getActivity()

爲什麼這樣嗎?實際上有2個獨立的佈局顯示在屏幕上。主人顯示一切,包括一個片段。片段基本上是一個獨立的迷你活動,在平板電腦開發中非常有用,即使只是用於電話類型的設備也很有用。您的佈局位於fragment_quiz xml文件中,該文件僅在onCreateView語句中的片段內充氣。只要您在充氣後使用findViewById,您就會安然無恙。

+0

我有兩個Eclipse生成的xml文件。 fragment_quiz.xml和activity_xml。主人是一個activity_xml嗎?另外PlaceHolderFragment類在哪裏? –

+1

搜索它,你將它包含在你的代碼示例中。主人是activity_xml,你會注意到它包含一個片段,最終就是事物所在。嘗試閱讀Android Fragment文檔,http://developer.android.com/guide/components/fragments.html – PearsonArtPhoto

+0

哦,我很盲目的x.x.感謝您的鏈接,我會閱讀它。 –