vvThat
vvThat

Reputation: 167

java.lang.NullPointerException and OnClickListener

this is my LogCat's log.

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

this is Main Activity codes.

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


    }

}

main activity's xml file.

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

this is other activity(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 's xml file.

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

    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 's xml file..

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

Hi,

When I run this app, forced stop. I guess this error's cause is OnClickListener. delete OnClickListener, this app is run. but I must need OnClickListener...

Upvotes: 0

Views: 270

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

You have this

setContentView(R.layout.board_wow);

And you have this in 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" />

Change this

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

to

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

Upvotes: 0

Bogdan Alexandru
Bogdan Alexandru

Reputation: 5552

The button's name is boardWrite_lol not bbb

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

BoardrActivity_wow has not Button with id bbb, you have to use the one you declared inside the Activity's layout (R.id.boardWrite_lol)

change

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

with

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

Upvotes: 2

Related Questions