2013-07-05 54 views
0

這是我的LogCat的日誌。java.lang.NullPointerException和OnClickListener

07-04 06:47:46.012: E/AndroidRuntime(2885): FATAL EXCEPTION: main 
07-04 06:47:46.012: E/AndroidRuntime(2885): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goodnight.ppt/com.goodnight.ppt.BoardActivity_wow}: java.lang.NullPointerException 

這是主要活動的代碼。

package com.goodnight.ppt; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

@SuppressWarnings("deprecation") 
public class BoardActivity_main extends TabActivity { 

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

     TabHost tabHost = getTabHost(); 


     //LOL tap 
     Intent intent1; 
     intent1 = new Intent().setClass(this, BoardActivity_lol.class); 
     TabSpec sp1 = tabHost.newTabSpec("Tab1"); 
     sp1.setIndicator("LOL"); 
     sp1.setContent(intent1); 
     tabHost.addTab(sp1); 

     //WOW tap 
     Intent intent2; 
     intent2 = new Intent().setClass(this, BoardActivity_wow.class); 
     TabSpec sp2 = tabHost.newTabSpec("Tab2"); 
     sp2.setIndicator("WOW"); 
     sp2.setContent(intent2); 
     tabHost.addTab(sp2); 

     //STAR tap 
     Intent intent3; 
     intent3 = new Intent().setClass(this, BoardActivity_star.class); 
     TabSpec sp3 = tabHost.newTabSpec("Tab3"); 
     sp3.setIndicator("STAR"); 
     sp3.setContent(intent3); 
     tabHost.addTab(sp3); 

     //WIND tap 
     Intent intent4; 
     intent4= new Intent().setClass(this, BoardActivity_wind.class); 
     TabSpec sp4 = tabHost.newTabSpec("Tab4"); 
     sp4.setIndicator("WIND"); 
     sp4.setContent(intent4); 
     tabHost.addTab(sp4); 


    } 

} 

主要活動的XML文件。

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

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       > 


      </FrameLayout> 



    </TabHost> 

</LinearLayout> 

這是其他活動(BoardActivity_wow)..

package com.goodnight.ppt; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class BoardActivity_wow extends Activity implements OnClickListener{ 

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

     Button writeButton_wow = new Button(this); 
     writeButton_wow = (Button)findViewById(R.id.bbb); 

     writeButton_wow.setOnClickListener(this); 

    } 

    public void onClick(View v){ 
     Intent wow = new Intent(this, BoardActivity_wow_write.class); 
     startActivity(wow); 
    } 

} 

BoardrActivity_wow的xml文件。

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

    <Button 
     android:id="@+id/boardWrite_lol" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Write_lol" /> 

</LinearLayout> 

boardactivity_wow_write活動..

package com.goodnight.ppt; 

import android.app.Activity; 
import android.os.Bundle; 

public class BoardActivity_wow_write extends Activity{ 

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

    } 

} 

BoardActivity_wow_write的XML文件..

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

    <EditText 
     android:id="@+id/title_wow" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" /> 

    <EditText 
     android:id="@+id/content_wow" 
     android:layout_width="match_parent" 
     android:layout_height="250dp" 
     android:ems="10" 
     android:inputType="textMultiLine" > 

    </EditText> 

    <Button 
     android:id="@+id/bbb" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="boardWrite_wow" /> 

</LinearLayout> 

嗨,

當我運行這個應用程序,強制停止。我猜這個錯誤的原因是OnClickListener。刪除OnClickListener,這個程序運行。但我必須需要OnClickListener ...

+0

是否將'BoardActivity_wow_write'添加到'manifest'? – R9J

回答

2

BoardrActivity_wow還沒有按鍵採用ID bbb,你必須使用你的活動佈局內聲明的一個(R.id.boardWrite_lol

變化

Button writeButton_wow = new Button(this); 
writeButton_wow = (Button)findViewById(R.id.bbb); 

Button writeButton_wow = (Button)findViewById(R.id.boardWrite_lol); 
0

該按鈕的名稱是boardWrite_lolbbb

+0

你是什麼意思?我只是解釋了OP爲什麼他的程序崩潰了 –

0

你有這樣的

setContentView(R.layout.board_wow); 

而且這個有board_wow.xml

<Button 
    android:id="@+id/boardWrite_lol" // id is boardWrite_lol 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Write_lol" /> 

更改此

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.board_wow); 
    Button writeButton_wow = new Button(this); // no need for this 
    writeButton_wow = (Button)findViewById(R.id.bbb); // change this statement. 
    writeButton_wow.setOnClickListener(this); 
    } 

 @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.board_wow); 
    Button writeButton_wow = (Button)findViewById(R.id.boardWrite_lol) 
    writeButton_wow.setOnClickListener(this); 
    }