0

我有一个非常简单的代码,因为我是 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
4

3 回答 3

1

你得到一个例外,因为你试图在膨胀视图之前引用 UI 元素。findViewById(..)只能在使用 . 链接包含布局的 xml 后才能使用setContentView(..)

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

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);
            }
        });
    }
    }
于 2012-09-29T18:23:33.607 回答
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)
   .....
于 2012-09-29T18:24:03.633 回答
1

setContentView似乎不见了。null你在做的时候得到一个findViewById

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

于 2012-09-29T18:33:38.617 回答