How can i get two of the same ImageButton methods to work?
package com.ave;
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;
}
}});
}
}
The code itself has no errors. Same for the xml, no errors. The class has been inserted into the androidManifest file as well. However, the app closes right after the splash screen is destroyed. What am i doing wrong?
'<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/droid_background">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="@string/hello"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<ImageButton
android:src="@drawable/select"
android:id="@+id/select"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
<ImageButton
android:src="@drawable/audio"
android:id="@+id/audio"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout> '
=================LogCat===================
E/AndroidRuntime( 352): FATAL EXCEPTION: main
E/AndroidRuntime( 352): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ave/com.ave.Menu}: java.lang.NullPointerException
E/AndroidRuntime( 352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
E/AndroidRuntime( 352): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRun开发者_如何转开发time( 352): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 352): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 352): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 352): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 352): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 352): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 352): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 352): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 352): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 352): at android.app.Activity.findViewById(Activity.java:1647)
E/AndroidRuntime( 352): at com.ave.Menu.(Menu.java:41)
E/AndroidRuntime( 352): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 352): at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime( 352): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime( 352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime( 352): ... 11 more
================End======================
I'm aware the LogCat is flagging line 35 or the code but if i try to remove it. My second ImageButton method screws up.
Try the following code I only put your block code into method. hope this will help.
package com.ave;
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;
ImageButton audio;
int isClicked1 = 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;
}
}
});
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;
}
}
});
}
}
精彩评论