嘿,我刚开始涉足 android 编程,从我短暂的时间来看,我有点沮丧。我在 Eclipse 中不会出错,但是当我启动我的应用程序时,它会立即强制关闭。我正在逐字阅读android教程,但我仍然遇到错误。我的目标是在相对布局中创建一个列表视图。此外,由于某种原因,当我分配 p.firstName 或 p.lastName 时,它也会强制关闭,但无论如何这是我的代码。如果这有所作为,我正在使用 API 级别 15(即 Android 4.0.3)。
package matthews.zack.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class _testActivity extends Activity {
Button save;
People p = new People();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//onInitialize();
}
private void onInitialize()
{
save = (Button)findViewById(android.R.id.button1);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText firstName = (EditText)findViewById(android.R.id.text1);
EditText lastName = (EditText)findViewById(android.R.id.text2);
p.setFirstName("Hai");//firstName.getText().toString());
p.setLastName("Hi");//lastName.getText().toString());
//((EditText)findViewById(android.R.id.text1)).setText("This the tune bada bing bada boom");
}
});
}
}
`
这是我的 XML 代码
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height = "fill_parent">
<TableLayout
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/firstName" />
<EditText
android:id="@+id/editText1"
android:ems="10"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView2"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:text="@string/lastName" />
<EditText
android:id="@+id/editText2"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Save" />
</TableRow>
</TableLayout>
<ListView
android:id = "@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@id/details"
/>
</RelativeLayout>
`
这是我的错误输出
06-12 00:32:44.871: D/AndroidRuntime(30530): Shutting down VM
06-12 00:32:44.871: W/dalvikvm(30530): threadid=1: thread exiting with uncaught exception (group=0x40a3b1f8)
06-12 00:32:44.879: E/AndroidRuntime(30530): FATAL EXCEPTION: main
06-12 00:32:44.879: E/AndroidRuntime(30530): java.lang.RuntimeException: Unable to start activity ComponentInfo{matthews.zack.test/matthews.zack.test._testActivity}: java.lang.NullPointerException
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.os.Looper.loop(Looper.java:137)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-12 00:32:44.879: E/AndroidRuntime(30530): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 00:32:44.879: E/AndroidRuntime(30530): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 00:32:44.879: E/AndroidRuntime(30530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-12 00:32:44.879: E/AndroidRuntime(30530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-12 00:32:44.879: E/AndroidRuntime(30530): at dalvik.system.NativeStart.main(Native Method)
06-12 00:32:44.879: E/AndroidRuntime(30530): Caused by: java.lang.NullPointerException
06-12 00:32:44.879: E/AndroidRuntime(30530): at matthews.zack.test._testActivity.onInitialize(_testActivity.java:24)
06-12 00:32:44.879: E/AndroidRuntime(30530): at matthews.zack.test._testActivity.onCreate(_testActivity.java:18)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.Activity.performCreate(Activity.java:4465)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-12 00:32:44.879: E/AndroidRuntime(30530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-12 00:32:44.879: E/AndroidRuntime(30530): ... 11 more
android会在不告诉我异常的情况下强制关闭,这有点令人沮丧。我将不胜感激。谢谢!