首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OnClickListener与ImageButton

OnClickListener与ImageButton
EN

Stack Overflow用户
提问于 2014-04-14 18:38:20
回答 2查看 968关注 0票数 0

我想创建一个小应用程序,它显示了几张图片,如果你点击它,一个敬酒弹出并注明名称。我真搞不懂它为什么会立刻坠毁。LogCat说了些类似于Nullpointerexpection的话?!谢谢你提前给我的帮助。

代码语言:javascript
复制
  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);}

     }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-14 18:57:19

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

注意该方法是如何被称为findViewById的,因此您实际上应该向它提供注册到图像按钮的ID。

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

setContentView(R.layout.activity_main);

这将设置屏幕,并将所有这些按钮放在那里,在您打完这个电话后,您需要获取对所有图像按钮的引用。

对于所有项目,layout/activity_main.xml都应该有一个类似于此的图像按钮

代码语言:javascript
复制
<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();

票数 1
EN

Stack Overflow用户

发布于 2014-04-14 18:49:21

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

代码语言:javascript
复制
@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.
          }
       }));
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23067688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档