在eclipse中,android应用程序在声明变量时崩溃

时间:2012-09-29 18:20:01

标签: android eclipse variables views logcat

我有一个非常简单的代码,因为我是Java和Android的新手。所以,我有这个按钮,点击它时,应该在两个文本视图之间切换文本。 我的Android手机已插入调试模式,因此我的应用程序直接在其上运行。然而,它一发布就崩溃了,说:“不幸的是,已经停止了。”

这是我的代码:

    public class ConvertItAll extends Activity {

String temp;
TextView tv1 = (TextView) findViewById(R.id.tvUnit1);
TextView tv2 = (TextView) findViewById(R.id.tvUnit2);

EditText et1=(EditText) findViewById(R.id.etInput);
TextView tv3=(EditText) findViewById(R.id.tvOutput);
Button b1=(Button) findViewById(R.id.bEdit1);
Button b2=(Button) findViewById(R.id.bEdit2);
Button b3 = (Button) findViewById(R.id.bSwap);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    b3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            temp = tv1.getText().toString();
            tv1.setText(tv2.getText().toString());
            tv2.setText(temp);
        }
    });
}
}

当我在Logcat中看到时,错误发生在我声明和初始化View变量的行中。所以,我试着评论这些声明(当然也没有使用变量),并且应用程序加载正常,我可以看到布局。

有人可以帮忙吗?如果我无法初始化变量,我将如何使用它们?

这是LogCat结果:

    •   09-29 23:42:10.788: I/Process(600): Sending signal. PID: 600 SIG: 9

    •   09-29 23:42:27.846: E/Trace(670): error opening trace file: No such file or directory (2)

    •   09-29 23:42:28.536: D/dalvikvm(670): GC_FOR_ALLOC freed 70K, 3% free 8051K/8259K, paused 51ms, total 53ms
    •   09-29 23:42:28.556: I/dalvikvm-heap(670): Grow heap (frag case) to 8.827MB for 960016-byte allocation

    •   09-29 23:42:28.776: D/dalvikvm(670): GC_CONCURRENT freed 1K, 3% free 8987K/9223K, paused 78ms+6ms, total 222ms

    •   09-29 23:42:28.776: D/dalvikvm(670): WAIT_FOR_CONCURRENT_GC blocked 8ms

    •   09-29 23:42:29.106: D/gralloc_goldfish(670): Emulator without GPU emulation detected.

    •   09-29 23:42:29.966: D/AndroidRuntime(670): Shutting down VM

    •   09-29 23:42:29.966: W/dalvikvm(670): threadid=1: thread exiting with uncaught exception (group=0x40a13300)

    •   09-29 23:42:29.996: E/AndroidRuntime(670): FATAL EXCEPTION: main

    •   09-29 23:42:29.996: E/AndroidRuntime(670): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.allinoneconverter/com.example.allinoneconverter.ConvertItAll}: java.lang.NullPointerException

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.access$600(ActivityThread.java:130)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.os.Handler.dispatchMessage(Handler.java:99)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.os.Looper.loop(Looper.java:137)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.main(ActivityThread.java:4745)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.reflect.Method.invokeNative(Native Method)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.reflect.Method.invoke(Method.java:511)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at dalvik.system.NativeStart.main(Native Method)

    •   09-29 23:42:29.996: E/AndroidRuntime(670): Caused by: java.lang.NullPointerException

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.Activity.findViewById(Activity.java:1825)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at com.example.allinoneconverter.ConvertItAll.<init>(ConvertItAll.java:12)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.Class.newInstanceImpl(Native Method)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.Class.newInstance(Class.java:1319)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  ... 11 more

3 个答案:

答案 0 :(得分:1)

您正在获取异常,因为您试图在膨胀视图之前引用UI元素。 findViewById(..) 只能在使用 setContentView(..) 关联包含布局的xml后使用。

尝试下面的代码,一切都应该正常。

public class ConvertItAll extends Activity {

    String temp;
    TextView tv1, tv2, tv3;
    EditText et1;
    Button b1, b2, b3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayoutXMLFileName); // you need to inflate the view before referencing the UI elements 
        tv1 = (TextView) findViewById(R.id.tvUnit1);
        tv2 = (TextView) findViewById(R.id.tvUnit2);
        et1=(EditText) findViewById(R.id.etInput);
        tv3=(EditText) findViewById(R.id.tvOutput);
        b1=(Button) findViewById(R.id.bEdit1);
        b2=(Button) findViewById(R.id.bEdit2);
        b3 = (Button) findViewById(R.id.bSwap);
        b3.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                temp = tv1.getText().toString();
                tv1.setText(tv2.getText().toString());
                tv2.setText(temp);
            }
        });
    }
    }

答案 1 :(得分:1)

在充气后,您只能在布局中获得对视图的引用。尝试在super()调用之后调用setContentView(),然后初始化变量。所以你的代码看起来像是:

public class ConvertItAll extends Activity {

String temp;
TextView tv1;

.....

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.<your_Llayout>);
    tv1 = (TextView) findViewById(R.id.tvUnit1)
   .....

答案 2 :(得分:1)

似乎缺少

setContentView。您在执行null时获得findViewById

读: http://developer.android.com/reference/android/app/Activity.html#setContentView(android.view.View