最近,我尝试放置一个图像按钮,该按钮可以相对于所用智能手机的分辨率进行自我替换。但是当我启动它时,它就不起作用了。我真的不明白发生了什么,但如果你能帮助我,我会很高兴。
这是我的java代码:
package com.example.snakesmash;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageButton play = (ImageButton) findViewById(R.id.play);
ImageView background = (ImageView) findViewById(R.id.backgroundMenue);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
play.layout((int) Math.round(background.getWidth() * 0.65), (int) Math.round(background.getHeight() * 0.65), (int) Math.round(background.getWidth() * 0.35), (int) Math.round(background.getHeight() * 0.35));
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是 XML 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/backgroundMenue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Nothing"
android:scaleType="centerCrop"
android:src="@drawable/main_menu2" />
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
最后是 logcat :
11-12 19:14:51.505: D/AndroidRuntime(219): Shutting down VM
11-12 19:14:51.505: W/dalvikvm(219): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-12 19:14:51.505: E/AndroidRuntime(219): Uncaught handler: thread main exiting due to uncaught exception
11-12 19:14:51.505: D/ddm-heap(219): Got feature list request
11-12 19:14:51.515: E/AndroidRuntime(219): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.snakesmash/com.example.snakesmash.MainActivity}: java.lang.NullPointerException
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.os.Looper.loop(Looper.java:123)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-12 19:14:51.515: E/AndroidRuntime(219): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 19:14:51.515: E/AndroidRuntime(219): at java.lang.reflect.Method.invoke(Method.java:521)
11-12 19:14:51.515: E/AndroidRuntime(219): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-12 19:14:51.515: E/AndroidRuntime(219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-12 19:14:51.515: E/AndroidRuntime(219): at dalvik.system.NativeStart.main(Native Method)
11-12 19:14:51.515: E/AndroidRuntime(219): Caused by: java.lang.NullPointerException
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.Activity.findViewById(Activity.java:1612)
11-12 19:14:51.515: E/AndroidRuntime(219): at com.example.snakesmash.MainActivity.<init>(MainActivity.java:11)
11-12 19:14:51.515: E/AndroidRuntime(219): at java.lang.Class.newInstanceImpl(Native Method)
11-12 19:14:51.515: E/AndroidRuntime(219): at java.lang.Class.newInstance(Class.java:1479)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-12 19:14:51.515: E/AndroidRuntime(219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
11-12 19:14:51.515: E/AndroidRuntime(219): ... 11 more
11-12 19:14:51.555: I/dalvikvm(219): threadid=7: reacting to signal 3
11-12 19:14:51.555: E/dalvikvm(219): Unable to open stack trace file '/data/anr/traces.txt': Permission denied