我想创建一个小应用程序,它显示几张图片,如果你点击它,就会弹出一个吐司并说出名字。我真的不明白为什么它会立即崩溃。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);}
}
}