Android应用程序因java.lang.NullPointerException关闭

Android应用程序因java.lang.NullPointerException关闭,android,Android,这是一个似乎已被广泛涵盖的话题,但我无法找出是什么导致了我的问题。我在我的应用程序上使用了一个选项卡布局,它们都可以正常工作,只有一个例外: E/AndroidRuntime(1190): Caused by: java.lang.NullPointerException E/AndroidRuntime(1190): at com.package.Example.onCreate(clas.java:62) E/AndroidRuntime(1190): at android.app.Ins

这是一个似乎已被广泛涵盖的话题,但我无法找出是什么导致了我的问题。我在我的应用程序上使用了一个选项卡布局,它们都可以正常工作,只有一个例外:

E/AndroidRuntime(1190): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1190): at com.package.Example.onCreate(clas.java:62)
 E/AndroidRuntime(1190): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
我已经找到了导致问题的那一行(62),但我感到困惑,因为我在其他5个实例中以同样的方式对其进行了编码。我发现java.lang.NullPointerException在未引用某些内容时调用,但事实并非如此,因为我在onCreate中引用了它。这是我的课程,粗体的线条导致了我的问题。谢谢你的帮助

public class Example extends Activity implements OnClickListener{

    ImageButton image1;
    ImageButton glow1;
    WebView webview1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.examplelayout);

        final ImageButton image1 = (ImageButton)findViewById(R.id.webgoogle);
        final ImageButton glow1 = (ImageButton)findViewById(R.drawable.googleglow);
        final WebView webview1 = (WebView)this.findViewById(R.id.webView6);
        final MediaPlayer sound = MediaPlayer.create(Youtube.this, R.raw.appsound);

        image1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                webview1.loadUrl("http://www.google.com");
                if(sound.isPlaying()){
                image1.setVisibility(ImageButton.VISIBLE);
                glow1.setVisibility(ImageButton.GONE);




            }else {
                sound.start();
                image1.setVisibility(ImageButton.GONE);
                glow1.setVisibility(ImageButton.VISIBLE);

            }
            }
        });

        **glow1.setOnClickListener(new View.OnClickListener() {**

            public void onClick(View v) {
                // TODO Auto-generated method stub
            webview1.loadUrl("http://www.google.com");
            if(sound.isPlaying()){
            glow1.setVisibility(ImageButton.GONE);
            image1.setVisibility(ImageButton.VISIBLE);

        }else {
            sound.start();
            glow1.setVisibility(ImageButton.VISIBLE);
            image1.setVisibility(ImageButton.GONE);
        }
        }
        });
发现了你的问题

 final ImageButton glow1 = (ImageButton)findViewById(R.drawable.googleglow);
这应该是,

 final ImageButton glow1 = (ImageButton)findViewById(R.id.googleglow);
您使用Drawable和id错误地铸造res。

发现了您的问题

 final ImageButton glow1 = (ImageButton)findViewById(R.drawable.googleglow);
这应该是,

 final ImageButton glow1 = (ImageButton)findViewById(R.id.googleglow);

您使用Drawable和id错误地铸造了res。

听起来像
(ImageButton)findViewById(R.Drawable.googleglow)
它返回空值。你调试过吗?听起来像是
(ImageButton)findViewById(R.drawable.googleglow)
它返回空值。你调试过了吗?我先将图像设置为(R.id.googleglow),然后将其更改为R.drawable.googleglow。它们都返回相同的错误。更改为id本身。尝试执行项目->清洁和检查。如果仍然存在错误,请检查examplelayout.xml以了解是否有id为“googleglow.wird”的ImageButton。。我将r.drawable.googleglow设置回r.id.googleglow,并注释掉任何引用googleglow且似乎有效的内容。我不确定到底发生了什么,但一切都很好。非常感谢。我先将图像设置为(R.id.googleglow),然后将其更改为R.drawable.googleglow。它们都返回相同的错误。更改为id本身。尝试执行项目->清洁和检查。如果仍然存在错误,请检查examplelayout.xml以了解是否有id为“googleglow.wird”的ImageButton。。我将r.drawable.googleglow设置回r.id.googleglow,并注释掉任何引用googleglow且似乎有效的内容。我不确定到底发生了什么,但一切都很好。非常感谢。