我想創建一個Android程序,它將讀取文本,然後將其打印出來,然後習慣使用Android的I/O。但是,每當我在AVD上運行它時,它會在LogCat中返回一個錯誤,說我的代碼中有一個NullPointerException,因此它「無法實例化活動ComponentInfo」。Android活動 - NullPointerException(「無法實例化活動ComponentInfo」)?
我找不到我指的是什麼空。
這裏是我的代碼:
import android.app.*;
import android.os.*;
import android.view.View;
import android.view.View.*;
import android.widget.*;
public class ButtonTestActivity extends Activity {
TextView tv = (TextView) findViewById(R.id.textView1);
EditText et = (EditText) findViewById(R.id.edittext);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.Enter);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tv.setText(et.getText().toString());
}
});
}
}
這裏是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/Enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
有誰知道什麼可能會造成這個?非常感謝。
我看不出有什麼明顯的錯誤。你可以粘貼來自LogCat – Sid 2012-03-02 16:55:11
的異常跟蹤帖子錯誤stacktrace – kosa 2012-03-02 16:55:30