Android Button onClickListener抛出一个空异常

时间:2014-07-11 05:30:33

标签: android button onclicklistener

我无法为onclicklistener设置Buttons。虽然其中一些工作正常,但其他人没有工作。代码显示没有错误,但是当我运行应用程序时,Log中出现错误,其中一个显示NullPointerException按钮而不是匿名按钮,但这告诉我void {{1 }}不是变量的有效类型。我已经尝试为onClick()设置XML代码来调用代码中定义的Button,但这也会在运行时抛出错误。我也尝试在onClick()之外声明onClick()但是在运行时也会抛出错误。无效的按钮是homeb01。我被卡住了,我的代码在下面,我真的可以使用一些帮助。谢谢。

onCreate()

不工作的按钮就是这个,homeb01

package com.example.deenbuddy;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;


public class MainActivity extends Activity 
{

MainActivity context;

//Arrays for RowItem values
final Integer[] uname = new Integer[] {R.string.temp_uname0,R.string.temp_uname0,R.string.temp_uname0,R.string.temp_uname0};
final Integer[] ustatus = new Integer[]{R.string.temp_ustatus0,R.string.temp_ustatus0,R.string.temp_ustatus0,R.string.temp_ustatus0};
final Integer[] upic = new Integer[]{R.drawable.tig0,R.drawable.tig0,R.drawable.tig0,R.drawable.tig0};

//ListView and RowItem for ListView
ListView listview;
List<RowItem> rowItem;

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

    context = this;

    //Navigation Buttons
    final Button homeb0 = (Button) findViewById(R.id.home_b0);
    final Button newsb0 = (Button) findViewById(R.id.news_b0);
    final Button featb0 = (Button) findViewById(R.id.feature_b0);
    final Button homeb01 = (Button) findViewById(R.id.home_b1);
    final Button newsb01 = (Button) findViewById(R.id.news_b1);
    final Button featb01 = (Button) findViewById(R.id.feature_b1);

    //News Buttons 
    final ImageView newsb1 = (ImageView) findViewById(R.id.news1);
    final ImageView newsb2 = (ImageView) findViewById(R.id.news2);
    final ImageView newsb3 = (ImageView) findViewById(R.id.news3);
    final ImageView newsb4 = (ImageView) findViewById(R.id.news4);

    //Feature Buttons
    final ImageView featb1 = (ImageView) findViewById(R.id.feat1);
    final ImageView featb2 = (ImageView) findViewById(R.id.feat2);
    final ImageView featb3 = (ImageView) findViewById(R.id.feat3);
    final ImageView featb4 = (ImageView) findViewById(R.id.feat4);

    //OnClicks



    //OnClick for Navigation
    //h_home
    homeb0.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view2)
        {
            setContentView(R.layout.activity_main);
        }
    });
    //f_home

我的LogCat如下:

    homeb01.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
             setContentView(R.layout.activity_main);

        }
    });

    //n_home
    //h_news
    newsb0.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v)
        {
            setContentView(R.layout.newstitle);
        }
    });
    //h_feat
    featb0.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v)
        {
            setContentView(R.layout.featurestitle);
        }
    });

    //OnClicks for news
    newsb1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.newstitle);
        }
    });

    newsb2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.newstitle);
        }
    });

    newsb3.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.newstitle);
        }
    });

    newsb4.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.newstitle);
        }
    });

    //OnClicks for feat
    featb1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.featurestitle);
        }
    });

    featb2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.featurestitle);
        }
    });

    featb3.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.featurestitle);
        }
    });

    featb4.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            setContentView(R.layout.featurestitle);
        }
    });



    rowItem = new ArrayList<RowItem>();
    for(int x = 0; x < uname.length; x++)
    {
        RowItem item = new RowItem(upic[x], uname[x], ustatus[x]);
        rowItem.add(item);
    }

    listview = (ListView) findViewById(R.id.sfeed);
    CListView adapter = new CListView(this,R.layout.sfeed_list,rowItem);
    listview.setAdapter(adapter);


}


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





}

2 个答案:

答案 0 :(得分:0)

尝试这样做

 //Navigation Buttons
    final Button homeb0 = (Button) findViewById(R.id.home_b0);
    final Button newsb0 = (Button) findViewById(R.id.news_b0);
    final Button featb0 = (Button) findViewById(R.id.feature_b0);
    final Button homeb01 = (Button) findViewById(R.id.home_b1);
    final Button newsb01 = (Button) findViewById(R.id.news_b1);
    final Button featb01 = (Button) findViewById(R.id.feature_b1);

    //News Buttons 
    final ImageView newsb1 = (ImageView) findViewById(R.id.news1);
    final ImageView newsb2 = (ImageView) findViewById(R.id.news2);
    final ImageView newsb3 = (ImageView) findViewById(R.id.news3);
    final ImageView newsb4 = (ImageView) findViewById(R.id.news4);

    //Feature Buttons
    final ImageView featb1 = (ImageView) findViewById(R.id.feat1);
    final ImageView featb2 = (ImageView) findViewById(R.id.feat2);
    final ImageView featb3 = (ImageView) findViewById(R.id.feat3);
    final ImageView featb4 = (ImageView) findViewById(R.id.feat4);

    //OnClicks
    homeb0.setOnClickListener(this);
    newsb0.setOnClickListener(this);
    featb0.setOnClickListener(this);
    newsb01.setOnClickListener(this);
    homeb01.setOnClickListener(this);
    featb01.setOnClickListener(this);
    newsb1.setOnClickListener(this);
    newsb2.setOnClickListener(this);
    newsb3.setOnClickListener(this);
    newsb4.setOnClickListener(this);
    featb1.setOnClickListener(this);
    featb2.setOnClickListener(this);
    featb3.setOnClickListener(this);
    featb4.setOnClickListener(this);




  }
@Override
public void onClick(View arg0) {
     switch (arg0.getId()) {
    case R.id.home_b0:
        //Code
        break;
    case R.id.newsb0:
        //Code
        break;

    case R.id.featb0:
        //Code
        break;
    case R.id.newsb01:
        //Code
        break;
    case R.id.homeb01:
        //Code
        break;
    case R.id.featb01:
        //Code
        break;
    case R.id.home_b0:
        //Code
        break;
    case R.id.newsb1:
        //Code
        break;
    case R.id.newsb2:
        //Code
        break;
    case R.id.newsb3:
        //Code
        break;
    case R.id.newsb4:
        //Code
        break;
    case R.id.featb1:
        //Code
        break;
    case R.id.featb2:
        //Code
        break;


    default:
        break;
    }

}

以及为什么setContentView(R.layout.activity_main);在内部按钮点击事件你能告诉你究竟想做什么

答案 1 :(得分:0)

确保xml中“home_b1”id的声明如下:

<Button
android:id="@+id/home_b1"/>

在id之前不要错过“+”。