0

这是我的 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 ...

4

3 回答 3

2

BoardrActivity_wow没有带有 id 的 Button bbb,您必须使用您在 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);
于 2013-07-05T08:39:36.010 回答
0

按钮的名称boardWrite_lol不是bbb

于 2013-07-05T08:41:25.443 回答
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);
    }
于 2013-07-05T08:41:50.273 回答