【问题标题】:OnClickListener with ImageButton带有 ImageButton 的 OnClickListener
【发布时间】:2014-04-15 22:04:10
【问题描述】:

我想创建一个小应用程序,它显示几张图片,如果你点击它,就会弹出一个吐司并说出名字。我真的不明白为什么它会立即崩溃。 LogCat 说类似 Nullpointerexpection 的东西?!非常感谢您的帮助。

  package com.example.housesgot;
  import android.os.Bundle;
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ImageButton;
  import android.widget.Toast;

  @SuppressLint("ShowToast")
  public class MainActivity extends Activity implements OnClickListener 
  {

      ImageButton  imageButton1,imageButton2,imageButton3,imageButton4,imageButton5,imageButton6;
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageButton1=(ImageButton)findViewById(R.drawable.house_baratheon);
            imageButton2=(ImageButton)findViewById(R.drawable.house_frey);
            imageButton3=(ImageButton)findViewById(R.drawable.house_greyjoy);
            imageButton4=(ImageButton)findViewById(R.drawable.house_lannister);
            imageButton5=(ImageButton)findViewById(R.drawable.house_stark);
            imageButton6=(ImageButton)findViewById(R.drawable.house_targaryen);
            imageButton1.setOnClickListener(this);
            imageButton2.setOnClickListener(this);
            imageButton3.setOnClickListener(this);
            imageButton4.setOnClickListener(this);
            imageButton5.setOnClickListener(this);
            imageButton6.setOnClickListener(this);
      }


    @SuppressLint("ShowToast")
    @Override
    public void onClick(View v) 
    {
        if(v==imageButton1){
            Toast.makeText(MainActivity.this, R.string.baratheon, Toast.LENGTH_LONG);}
        if(v==imageButton2){
            Toast.makeText(MainActivity.this, R.string.frey, Toast.LENGTH_LONG);}
        if(v==imageButton3){
            Toast.makeText(MainActivity.this, R.string.greyjoy, Toast.LENGTH_LONG);}
        if(v==imageButton4){
            Toast.makeText(MainActivity.this, R.string.lannister, Toast.LENGTH_LONG);}
        if(v==imageButton5){
            Toast.makeText(MainActivity.this, R.string.stark, Toast.LENGTH_LONG);}
        if(v==imageButton6){
            Toast.makeText(MainActivity.this, R.string.targaryen, Toast.LENGTH_LONG);}

     }
}

【问题讨论】:

  • 可能是你的变量在你尝试做findViewById时永远不会被赋值,原因可能是它还没有完成xml布局的设置

标签: android onclicklistener android-imagebutton


【解决方案1】:

imageButton1 = (ImageButton) findViewById(R.drawable.house_baretheon);

注意这个方法是如何被调用的findViewById,所以你实际上应该向它提供你注册到图像按钮的 ID

您必须先设置活动的内容视图

setContentView(R.layout.activity_main);

这将设置屏幕,并将所有这些按钮放在那里,在您进行此调用后,您需要获取对所有图像按钮的引用。

layout/activity_main.xml 的所有项目都应该有一个像这样的图像按钮

<ImageButton
    android:id="@+id/button_house_baretheon"
    android:src="@drawable/house_baretheon"
    ... />

然后通过调用获取引用

imageButton1 = (ImageButton) findViewById(R.id.button_house_baretheon)

您的代码的其余部分都很好。

编辑

哦,再等一件事,你需要在makeText()方法调用之后调用show(),所以像这样

Toast.makeText(MainActivity.this, R.string.baratheon, Toast.LENGTH_LONG).show();

【讨论】:

  • 所以我已经改变了你所说的两件事..一个有 id 和一个有字符串的东西。现在布局看起来不错,但如果我点击其中一张图片,什么也不会发生。老实说,我不知道如何准确地发布 logcat...
  • 工作正常!非常感谢!
【解决方案2】:

由于您没有发布任何 logcat,我无法更正您的代码,但我可以给您一个工作示例:

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


      ImageButton ib1,ib2,ib3,ib4,ib5;

       //change your image ID's here
       ib1= (ImageButton) findViewById(R.id.go_to_lagan_screen);
       ib2= (ImageButton) findViewById(R.id.go_to_city);
       ib3= (ImageButton) findViewById(R.id.go_to_university);
       ib4= (ImageButton) findViewById(R.id.go_to_icon_screen);
       ib5= (ImageButton) findViewById(R.id.map_to_home_screen);


       ib1.setOnClickListener(new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             //change the action here, a toast in your example
             Toast.makeText(MainActivity.this, MainActivity.this.getResources().getString(R.string.my_resource_string), Toast.LENGTH_LONG);

          }
       } );

       ib2.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent1= new Intent (MapScreen.this, CityCentre.class);
             startActivity(intent1);
             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
       ib3.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent2= new Intent (MapScreen.this, UniversityArea.class);
             startActivity(intent2);
             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
       ib4.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent3= new Intent (MapScreen.this, TheIcons.class);
             startActivity(intent3);

             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
       ib5.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent4= new Intent (MapScreen.this, MyActivity.class);
             startActivity(intent4);
             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
    }

【讨论】:

  • 实际上,Toast 类中的 makeText() 方法接受 String id 的
  • @user3533210 欢迎您!如果答案对您有帮助,请按向上/向下投票按钮下方的绿色标记。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 2021-04-04
  • 2012-07-19
相关资源
最近更新 更多