Android java syntax help
import com.ave.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class Menu extends Activity{
ImageButton select;
int isClicked = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
select = (ImageButton)findViewById(R.id.select);
select.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isClicked == 0){
select.setImageResource(R.drawable.select_pressed);
isClicked = 1;
}
else{
select.setImageResource(R.drawable.select);
isClicked = 0;
}
}});
}
ImageButton audio;
int isClicked1 = 0;{
audio = (ImageButton)findViewById(R.id.audio);
audio.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isClicked1 == 0){
audio.setImageResource(R.drawable.audio_pressed);
isClicked1 = 1;
}
else{
audio.setImageResource(R.drawable.audio);
isClicked1 = 0;
}
}
});
}
}
I'm not sure whats going wrong. There are no errors within the xml files or java class, however, when i go to test out the app via my phone or the AVD, after the splash screen it just randomly force closes.
LogCat
E AndroidRuntime: FATAL EXCEPTION: main
07-05 10:47:59.318 9362 9362 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ave/com.ave.Menu}: java.lang.NullPointerException
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1618)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.ActivityThread.access$1500(ActivityThread.java:124)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.os.Looper.loop(Looper.java:123)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:3806)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:507)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: Caused by: java.lang.NullPointerException
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.Activity.findViewById(Activity.java:1693)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at com.ave.Menu.<init>(Menu.java:41)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at java.lang.Class.newInstanceImpl(Native Method)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at java.lang.Class.newInstance(Clas开发者_C百科s.java:1409)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610)
07-05 10:47:59.318 9362 9362 E AndroidRuntime: ... 11 more
I think the issue here is that your onCreate is closed too early. Everything I see should be in the onCreate code block. Remove the } after the first onClick block (marked by }});) and add it back at the end of the file
Have you tried updating the AndroidManifest.xml
file with your Activity
s?
I think I have faced this same problem.
Usually this error occurs when particular resource is not found.
In your case, check whether following resource is present in xml file corresponding to your view.
select = (ImageButton)findViewById(R.id.select);
OR this resource
audio = (ImageButton)findViewById(R.id.audio);
Hope it helps.
Logcat output can help us to pinpoint the problem.
精彩评论