java.lang.NullPointerException和OnClickListener

时间:2013-07-05 08:37:30

标签: android onclicklistener

这是我的LogCat日志。

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 ......

3 个答案:

答案 0 :(得分:2)

BoardrActivity_wow没有ID为bbb的Button,您必须使用在Activity的布局中声明的那个(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);

答案 1 :(得分:0)

按钮的名称为boardWrite_lol而不是bbb

答案 2 :(得分: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);
    }