0

我不知道为什么,但是我在null pointer exceptions下面的代码中得到了这只是基本代码,但是一旦我单击其中一个图像,就会导致空指针异常

public class MainActivity extends Activity implements OnClickListener{

    ImageView img1,img2,img3,img4,img5,img6,img7,img8,img9;
    int i = 0;

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

    ImageView img1 = (ImageView) findViewById(R.id.ImageView01);
    ImageView img2 = (ImageView) findViewById(R.id.ImageView02);
    ImageView img3 = (ImageView) findViewById(R.id.ImageView03);
    ImageView img4 = (ImageView) findViewById(R.id.ImageView04);
    ImageView img5 = (ImageView) findViewById(R.id.ImageView05);
    ImageView img6 = (ImageView) findViewById(R.id.ImageView06);
    ImageView img7 = (ImageView) findViewById(R.id.ImageView07);
    ImageView img8 = (ImageView) findViewById(R.id.ImageView08);
    ImageView img9 = (ImageView) findViewById(R.id.ImageView09);     
    img1.setOnClickListener(this);       
        img2.setOnClickListener(this);     
        img3.setOnClickListener(this); 
        img4.setOnClickListener(this);
        img5.setOnClickListener(this); 
        img6.setOnClickListener(this);
        img7.setOnClickListener(this);
        img8.setOnClickListener(this);
        img9.setOnClickListener(this);

        if(i == 8){
            //show dialog
            Toast.makeText(getApplicationContext(), "You clicked all 9", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ImageView01:
            // do something
            img1.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView02:
            // do something
            img2.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView03:
            // do something     
            img3.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView04:
            // do something
            img4.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView05:
            // do something
            img5.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView06:
            // do something
            img6.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView07:
            // do something
            img7.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView08:
            // do something
            img8.setVisibility(View.GONE);
            i = i + 1;
            break;
        case R.id.ImageView09:
            // do something
            img9.setVisibility(View.GONE);
            i = i + 1;
            break;
        }
    }

日志猫:

E/AndroidRuntime(1399):致命异常:主要
E/AndroidRuntime(1399):java.lang.NullPointerException
E/AndroidRuntime(1399):在 com.aliancegaming.duckduckgone.MainActivity.onClick(MainActivity.java:49)
E/AndroidRuntime(1399):在 android.view.View.performClick(View.java:4084)
E/AndroidRuntime(1399):在 android.view.View$PerformClick.run(View.java:16966)
E/AndroidRuntime(1399):在 android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(1399):在 android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1399):在 android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1399):在 android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(1399):在 java.lang.reflect.Method.invokeNative(本机方法)
E/AndroidRuntime(1399):在 java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(1399):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(1399):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(1399):在 dalvik.system.NativeStart.main(本机方法)
4

5 回答 5

3

来自 XML 布局的所有 imageViews 引用都在之后 setContentView(R.layout.activity_main);

像,

ImageView img1,img2,img3,img4,img5,img6,img7,img8,img9; 

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

    img1 = (ImageView) findViewById(R.id.ImageView01);
    img2 = (ImageView) findViewById(R.id.ImageView02);
    img3 = (ImageView) findViewById(R.id.ImageView03);
    img4 = (ImageView) findViewById(R.id.ImageView04);
    img5 = (ImageView) findViewById(R.id.ImageView05);
    img6 = (ImageView) findViewById(R.id.ImageView06);
    img7 = (ImageView) findViewById(R.id.ImageView07);
    img8 = (ImageView) findViewById(R.id.ImageView08);
    img9 = (ImageView) findViewById(R.id.ImageView09);


    img1.setOnClickListener(this);       
    img2.setOnClickListener(this);     
    img3.setOnClickListener(this); 
    img4.setOnClickListener(this);
    img5.setOnClickListener(this); 
    img6.setOnClickListener(this);
    img7.setOnClickListener(this);
    img8.setOnClickListener(this);
    img9.setOnClickListener(this);
}
于 2012-07-28T07:10:21.837 回答
1
ImageView img1,img2,img3,img4,img5,img6,img7,img8,img9;
int i = 0;

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

img1 = (ImageView) findViewById(R.id.ImageView01);
img2 = (ImageView) findViewById(R.id.ImageView02);
img3 = (ImageView) findViewById(R.id.ImageView03);
img4 = (ImageView) findViewById(R.id.ImageView04);
img5 = (ImageView) findViewById(R.id.ImageView05);
img6 = (ImageView) findViewById(R.id.ImageView06);
img7 = (ImageView) findViewById(R.id.ImageView07);
img8 = (ImageView) findViewById(R.id.ImageView08);
img9 = (ImageView) findViewById(R.id.ImageView09); 
}
于 2012-07-28T08:39:14.977 回答
1

onClick() { }您正在使用img1 到 img9

它们是全局声明的对象,未在onCreate(){}中初始化

因为在 OnCreate() 中,您将新的iimg1 带到了 img9。

于 2012-07-28T08:40:29.093 回答
0

把这段代码放在setContentView(R.layout.activity_main)下面onCreate

img1 = (ImageView) findViewById(R.id.ImageView01);
img2 = (ImageView) findViewById(R.id.ImageView02);
img3 = (ImageView) findViewById(R.id.ImageView03);
img4 = (ImageView) findViewById(R.id.ImageView04);
img5 = (ImageView) findViewById(R.id.ImageView05);
img6 = (ImageView) findViewById(R.id.ImageView06);
img7 = (ImageView) findViewById(R.id.ImageView07);
img8 = (ImageView) findViewById(R.id.ImageView08);
img9 = (ImageView) findViewById(R.id.ImageView09);
于 2012-07-28T07:12:25.660 回答
0

在被调用findViewById()之前你不能调用setCoententView()

于 2012-07-28T07:12:41.297 回答