空指针异常的Andr​​oid应用程序

空指针异常的Andr​​oid应用程序

本文介绍了空指针异常的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但我收到空指针异常 wihin低于这个这个code是只有基本的code,但什么是导致空指针除了当我点击其中一个图像

 公共类MainActivity扩展活动实现OnClickListener {

    ImageView的IMG1,IMG2,IMG3,IMG4,IMG5,img6,IMG7,img8,img9;
    INT I = 0;

    @覆盖
    公共无效的onCreate(包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(本);
        img2.setOnClickListener(本);
        img3.setOnClickListener(本);
        img4.setOnClickListener(本);
        img5.setOnClickListener(本);
        img6.setOnClickListener(本);
        img7.setOnClickListener(本);
        img8.setOnClickListener(本);
        img9.setOnClickListener(本);

        如果(我== 8){
            //显示对话框
            Toast.makeText(getApplicationContext(),你点击的所有9,Toast.LENGTH_LONG).show();
        }
    }

    @覆盖
    公共无效的onClick(视图v){
        开关(v.getId()){
        案例R.id.ImageView01:
            // 做一点事
            img1.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView02:
            // 做一点事
            img2.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView03:
            // 做一点事
            img3.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView04:
            // 做一点事
            img4.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView05:
            // 做一点事
            img5.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView06:
            // 做一点事
            img6.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView07:
            // 做一点事
            img7.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView08:
            // 做一点事
            img8.setVisibility(View.GONE);
            I = I + 1;
            打破;
        案例R.id.ImageView09:
            // 做一点事
            img9.setVisibility(View.GONE);
            I = I + 1;
            打破;
        }
    }
 

logcat的:

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(本机方法)

解决方案

从XML布局中的所有imageViews refrences是继的setContentView(R.layout.activity_main);

一样,

  ImageView的IMG1,IMG2,IMG3,IMG4,IMG5,img6,IMG7,img8,img9;

@覆盖
公共无效的onCreate(包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(本);
    img2.setOnClickListener(本);
    img3.setOnClickListener(本);
    img4.setOnClickListener(本);
    img5.setOnClickListener(本);
    img6.setOnClickListener(本);
    img7.setOnClickListener(本);
    img8.setOnClickListener(本);
    img9.setOnClickListener(本);
}
 

I have no idea why but I am getting null pointer exceptions wihin this code below this is only basic code but something is causing a null pointer exception as soon as I click on one of the images

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

Logcat:

E/AndroidRuntime( 1399): FATAL EXCEPTION: main
E/AndroidRuntime( 1399): java.lang.NullPointerException
E/AndroidRuntime( 1399):    at com.aliancegaming.duckduckgone.MainActivity.onClick(MainActivity.java:49)
E/AndroidRuntime( 1399):    at android.view.View.performClick(View.java:4084)
E/AndroidRuntime( 1399):    at android.view.View$PerformClick.run(View.java:16966)
E/AndroidRuntime( 1399):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime( 1399):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1399):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 1399):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 1399):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1399):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 1399):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 1399):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 1399):    at dalvik.system.NativeStart.main(Native Method)
解决方案

All imageViews refrences from XML layout is after setContentView(R.layout.activity_main);

Like,

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

这篇关于空指针异常的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:30