有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何改革我的Android应用程序,使其与layout()函数一起工作

最近,我试着放置一个图像按钮,它可以根据所用智能手机的分辨率进行自我替换。 但当我启动它的时候,它就是不起作用。 我真的不明白发生了什么, 但是如果你能帮助我,我会很高兴的

以下是我的java代码:

package com.example.snakesmash;

import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.view.Menu;
import 安卓.widget.ImageButton;
import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <ImageView
        安卓:id="@+id/backgroundMenue"
        安卓:layout_width="fill_parent"
        安卓:layout_height="fill_parent"
        安卓:contentDescription="Nothing"
        安卓:scaleType="centerCrop"
        安卓:src="@drawable/main_menu2" />


    <ImageButton
        安卓:id="@+id/play"
        安卓:layout_width="wrap_content"
        安卓: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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
11-12 19:14:51.515: E/AndroidRuntime(219):  at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-12 19:14:51.515: E/AndroidRuntime(219):  at 安卓.app.ActivityThread.access$2200(ActivityThread.java:119)
11-12 19:14:51.515: E/AndroidRuntime(219):  at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-12 19:14:51.515: E/AndroidRuntime(219):  at 安卓.os.Handler.dispatchMessage(Handler.java:99)
11-12 19:14:51.515: E/AndroidRuntime(219):  at 安卓.os.Looper.loop(Looper.java:123)
11-12 19:14:51.515: E/AndroidRuntime(219):  at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-12 19:14:51.515: E/AndroidRuntime(219):  at com.安卓.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 安卓.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 安卓.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-12 19:14:51.515: E/AndroidRuntime(219):  at 安卓.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

共 (1) 个答案

  1. # 1 楼答案

    所有这些

    ImageButton play = (ImageButton) findViewById(R.id.play);
    ImageView background = (ImageView) findViewById(R.id.backgroundMenue);
    

    需要从你现在拥有的地方移除,然后放在后面

    setContentView(R.layout.activity_main);
    

    当你试图访问元素时,布局还没有膨胀。在setContentView之前不应该有任何内容(除了调用超级方法),所有布局元素都应该在那之后