This is a topic that seems to have been broadly covered, yet I cannot figure out what is causing my problem. I am using a tab layout on my application, and they all work fine with the exception of one that throws this:
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)
I have found the line that is causing the problem (62), but I am baffled because I have this coded the same way in 5 other instances. What I have found for a java.lang.NullPointerException is that this is called when something is not referenced, but this is not the case, as I have it referenced in my onCreate. Here is my class with the bolded line the one causing my issues. Thank you for your help!
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);
}
}
});
Found your problem,
This should be,
You are casting the res wrongly with Drawable and id.