2013-04-24 91 views
0

這是activity_main.xml中的文件:爲什麼自定義控件拋出NullPointerException?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layoutRoot" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/L1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" > 

     <Button 
      android:id="@+id/addButton" 
      android:layout_width="74dp" 
      android:layout_height="wrap_content" 
      android:text="Add" /> 

     <EditText 
      android:id="@+id/newEditText" 
      android:layout_width="174dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3.24" 
      android:ems="10" 
      android:inputType="text" /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/List" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    </ListView>  
</LinearLayout> 

這是命名爲second.xml定製化合物控制xml文件:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/jokeView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

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

      <Button 
       android:id="@+id/expandButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.44" 
       android:text = "One Click"/> 

      <TextView 
       android:id="@+id/txtView" 
       android:layout_width="253dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.56" 
       android:ems="10" 
       android:text = "LOL This is a sample code"/>  
     </LinearLayout> 

     <RadioGroup 
      android:id="@+id/ratingRadioGroup" 
      android:layout_width="321dp" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <RadioButton 
       android:id="@+id/likeButton" 
       android:layout_width="145dp" 
       android:layout_height="wrap_content" 
       android:checked="true" 
       android:text="Like" /> 

      <RadioButton 
       android:id="@+id/dislikeButton" 
       android:layout_width="147dp" 
       android:layout_height="wrap_content" 
       android:text="Dislike" /> 
     </RadioGroup> 
</LinearLayout> 

這是膨脹second.xml java文件:

public class Second extends View 
{ 
    private Button mExpandButton; 
    private RadioButton mLikeButton; 
    private RadioButton mDislikeButton; 
    private TextView mText; 
    private RadioGroup mLikeGroup; 
    private ViewGroup vGroup; 

    OnClickListener myClickListener = new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      String s= mExpandButton.getText().toString(); 

      if (s.equals("One Click")) 
       mExpandButton.setText("Other Click"); 
      else 
       mExpandButton.setText("One Click"); 
     } 
    }; 

    OnCheckedChangeListener myCkeckListener = new OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) 
     { 
      if (mLikeButton.isChecked()) 
       mText.setText("I Like it"); 

      if (mDislikeButton.isChecked()) 
       mText.setText("I Don't Like it"); 
     } 
    }; 

    public Second(Context context) 
    { 
     super(context); 
     mExpandButton = (Button) this.findViewById(R.id.expandButton); 
     mText = (TextView)this.findViewById(R.id.txtView); 
     mLikeGroup = (RadioGroup)this.findViewById(R.id.ratingRadioGroup); 
     mExpandButton.setOnClickListener(myClickListener); 
     mLikeGroup.setOnCheckedChangeListener(myCkeckListener); 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.second, null); 
    } 
    } 

最後,這是activity_main.xml中的Java代碼:

public class MainActivity extends Activity 
{ 
    protected ListView mLayout; 
    protected EditText mEditText; 
    protected Button mButton; 

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

     mLayout = (ListView)findViewById(R.id.List); 
     mEditText = (EditText)findViewById(R.id.newEditText); 
     mButton = (Button)findViewById(R.id.addButton); 

     for (int i =0; i< 4; i++) 
     { 
      Second sec = new Second(this); 
      mLayout.addView(sec); 
       setContentView(sec); 
     } 
    } 

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

這是生成的logcat(更新。我的包的名字是nullextry):

04-24 16:33:30.072: E/AndroidRuntime(1759): FATAL EXCEPTION: main 
04-24 16:33:30.072: E/AndroidRuntime(1759): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nullextry/com.example.nullextry.MainActivity}: java.lang.NullPointerException 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.os.Looper.loop(Looper.java:137) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at dalvik.system.NativeStart.main(Native Method) 
04-24 16:33:30.072: E/AndroidRuntime(1759): Caused by: java.lang.NullPointerException 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at com.example.nullextry.Second.<init>(Second.java:58) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at com.example.nullextry.MainActivity.onCreate(MainActivity.java:31) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.Activity.performCreate(Activity.java:5104) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-24 16:33:30.072: E/AndroidRuntime(1759):  ... 11 more 
04-24 16:33:30.192: D/dalvikvm(1759): GC_CONCURRENT freed 110K, 9% free 2858K/3112K, paused 79ms+5ms, total 232ms 
04-24 16:33:32.793: I/Process(1759): Sending signal. PID: 1759 SIG: 9 

調試時,我發現second.xml在second.java構造,其中聽衆被分配到各自的控制膨脹後的狀態則拋出該異常。 我錯過了什麼嗎? 我還沒有宣佈什麼?

愚蠢的錯誤:我沒有申報mExpandButton和mLikeGroup。我修復了second.java代碼,(謝謝鄒祖!)。但我得到同樣的例外(logcat已經更新)。

+0

我編輯了我的答案 – 2013-04-25 08:11:01

回答

1

您忘記在構造函數中初始化 mExpandButtonmLikeGroup

因此,當你這樣做的:

mExpandButton.setOnClickListener(myClickListener); 

mLikeGroup.setOnCheckedChangeListener(myCkeckListener); 

它拋出一個NPE

而且不要忘了初始化

mLikeButton; 
mDislikeButton; 
mText; 

其他

  if (mLikeButton.isChecked()) 
       mText.setText("I Like it"); 

      if (mDislikeButton.isChecked()) 
       mText.setText("I Don't Like it"); 

將拋出NullPointerExceptions了。

編輯:

你試圖誇大你的佈局之前,分配按鈕。嘗試改爲:

public Second(Context context) 
    { 
     super(context); 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.second, null); 
     mExpandButton = (Button) v.findViewById(R.id.expandButton); 
     mText = (TextView)v.findViewById(R.id.txtView); 
     mLikeGroup = (RadioGroup)v.findViewById(R.id.ratingRadioGroup); 
     mExpandButton.setOnClickListener(myClickListener); 
     mLikeGroup.setOnCheckedChangeListener(myCkeckListener); 
    } 
+0

我對代碼做了一些更改,現在我沒有得到任何異常,但是看不到我的自定義控件。這將是另一個問題。謝謝! – 2013-04-25 16:04:57

相關問題