0

我正在尝试编写这个简单的类,它将接受用户输入并将其显示到文本视图中,但每次我尝试运行它时,我的主类中都会出现致命错误。我是新手,我知道这很简单,但对于我的生活,我找不到它!非常感谢任何帮助。请参阅下面我的代码来创建类

 package com.ObComDis.wayne;

import android.app.Activity;
import android.os.Bundle;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ObComDisActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

String name;
EditText et = (EditText) findViewById(R.id.edittext);
TextView t = (TextView) findViewById(R.id.textView1);

public String getName()
{
    String name = et.getText().toString();
    return name;
}
public void main (String args [])
{
    ObComDisActivity myocd = new ObComDisActivity();
    t.setText(name);

}
}

这是我从日志猫那里得到的读数.....

07-11 18:55:01.149: D/AndroidRuntime(525): Shutting down VM
07-11 18:55:01.149: W/dalvikvm(525): threadid=1: thread exiting with uncaught exception       (group=0x40015560)
07-11 18:55:01.189: E/AndroidRuntime(525): FATAL EXCEPTION: main
07-11 18:55:01.189: E/AndroidRuntime(525): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ObComDis.wayne/com.ObComDis.wayne.ObComDisActivity}: java.lang.NullPointerException
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.os.Looper.loop(Looper.java:123)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.reflect.Method.invokeNative(Native Method)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.reflect.Method.invoke(Method.java:507)
07-11 18:55:01.189: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-11 18:55:01.189: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-11 18:55:01.189: E/AndroidRuntime(525):  at dalvik.system.NativeStart.main(Native Method)
07-11 18:55:01.189: E/AndroidRuntime(525): Caused by: java.lang.NullPointerException
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.Activity.findViewById(Activity.java:1647)
07-11 18:55:01.189: E/AndroidRuntime(525):  at com.ObComDis.wayne.ObComDisActivity.<init>(ObComDisActivity.java:25)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.Class.newInstanceImpl(Native Method)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.Class.newInstance(Class.java:1409)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
07-11 18:55:01.189: E/AndroidRuntime(525):  ... 11 more
4

1 回答 1

1

findViewById() 在创建该视图之前返回 null,在您的情况下,这只会作为 onCreate() 中的 setContentView() 的结果发生。

尝试这样的事情:

EditText et;
TextView t;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et = (EditText) findViewById(R.id.edittext);
    t = (TextView) findViewById(R.id.textView1);
}
于 2012-07-11T19:05:33.507 回答