2014-04-13 2 views
0

방금 ​​Android 프로그래밍 학습을 시작하여 문제가 발생했습니다. "Android 프로그래밍 The Big Nerd Ranch Guide"라는 책을 사용하고 있습니다. 내 IDE는 Eclipse와 Genymotion으로 앱을 에뮬레이션합니다.오류가 발생한 첫 번째 프로그램

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; 
     } 
    } 

} 

내가 뭘 : (Eclipse에서 가져온 코드의 대부분은)

<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 파일 : 여기

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 것 : 여기에서 로그 캣입니다 잘못된?

+1

원인 : java.lang.NullPointerException 04-13 00 : 19 : 48.785 : E/AndroidRuntime (1256) : at com.bignerdranch.android.geoquiz.QuizActivity.onCreate (QuizActivity.java:30) 줄을보세요. 30 변수를 null 값으로 찾은 다음 코드에서 역 추적하여 null인지 확인하십시오. – NormR

+0

mTrueButton은 null입니까? 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를 읽으십시오. 링크를 가져 주셔서 감사합니다, 나는 그것에 대해 읽을 것입니다. –

관련 문제