Android ToggleButton crash (resource NullPointerException)
I'm new to android and i've just been getting myself familiar with it and i've stumbled on this problem with fetching resources. I've added a toggle button on to the main layout but when i access the resource and then try to change text with button.setText()
the app crashes.
Here is the relevant code listing:
main.xml
<?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">
<com.games.tiktakboom.GameView
android:id="@+id/game"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ToggleButton android:layout_width="120px" android:id="@+id/toggleButton1" android:layout_height="120px" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:background="@drawable/bomb"></ToggleButton>
</RelativeLayout>
MainActivity.java
public class TikTakBoom extends Activity {
...
public ToggleButton startButton;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// turn off the window's title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// tell system to use the layout defined in our XML file
setContentView(R.layout.main);
// get handles to the LunarView from XML, and its LunarThread
mGameView = (GameView) findViewById(R.id.game);
mGameThread = mGameView.getThread();
// set up a new game
mGameThread.setState(GameThread.STATE_READY);
Log.w(this.getClass().getName(), "SIS is null");
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mGameThread, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
startButton = (ToggleButton)findViewById(R.id.toggleButton1);
startButton.setText("test");
}
...
Btw, this is a modified template i got on some forum or something, that's why the weird comments.
The app crashes as soon as i try to fetch te resource with findViewById
. If i put this button in another class the app crashes on setText()
. I have no idea why it doesn't fetch the resource correctly, a problem with the layout possibly?
Any help would be appreciated. Thanks. Cheers.
Val
FIRST PART SOLVED: CLEAN PROJECT AND RESTART ECLIPSE. (lol)
EDIT (still open for possible solutions): It seems that restarting Eclipse solved the problem (clean project and restarting didn't work before..), go figure.
But my question is sort of a follow up, if i fetch the button resource from another class it still crashes on setText()
or any other atempt to modify the button's properties (as far as debugger says it's also supposed to be NullPointerException). I've read somewhere that a button resource is only valid if fetched inside Activity class, tho i didn't believe it at the time. Is there any truth to this or better yet, any reason for that?
The relevant code segment:
class GameView extends SurfaceView implements SurfaceHolder.Callback {
class GameThread extends Thread implements SensorEventListener {
..
ToggleButton startButton;
..
public GameThread(SurfaceHolder surfaceHolder, Context context,
Handler handler) {
// get handles to some important objects
mSurfaceHolder = surfaceHolder;
mHandler = handler;
mContext = context;
mBomb = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bomb);
startButton = (ToggleButton)findViewById(R.id.toggleButton1);
}
..
/* Callback invoked when the surface dimensions change. */
public void setSurfaceSize(int width, int height) {
// synchronized to make sure these all change atomically
synchronized (mSurfaceHolder) {
mCanvasWidth = width;
mCanvasHeight = height;
}
mBomb = Bitmap.createScaledBitmap(mBomb, (int)(mCanvasHeight/4), (int)(mCanvasHeight/4), true);
startButton.setTextOn("test");
}
..
I know, this code doesn't make much sense, should work tho.. Any ideas as to why the previous atempt to fetch the resource works and not in another class? Tnx.
EDIT:
Here is my logcat, sorry for the delay.
08-17 00:48:46.891: WARN/dalvikvm(30139): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.games.tiktakboom/com.games.tiktakboom.TikTakBoom}: android.view.InflateException: Binary XML file line #6: Error inflating class com.games.tiktakboom.GameView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
at android.app.ActivityThread.access$2300(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.games.tiktakboom.GameView
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:200)
at android.app.Activity.setContentView(Activity.java:1647)
at com.games.tiktakboom.TikTakBoom.onCreate(TikTakBoom.java:100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at com.games.tiktakboom.GameView.<init>(GameView.java:354)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 21 more
Caused by: java.lang.NullPointerException
at com.games.tiktakboom.GameView$GameThread.<init>(GameView.java:101)
... 25 more
Force finishing activity com.games.tiktakboom/.TikTakBoom
As said, the log points to a NullPointerException (on the fetched resource). I can't make out the rest. Any help would be appreciated since i can't seem to be able to solve this problem. Thanks, cheers.
EDIT: Perhaps a problem with contexts? The context is passed to the constructor when a new thread is constructed. Do i have to manually fetch resources from the 开发者_高级运维passed context variable or something?
Instead, try:
startButton.setTextOn("test");
startButton.setTextOff("test");
精彩评论