嘿,我刚刚开始涉足android编程,从我短暂的时间开始,我有点沮丧。我可以在eclipse中没有错误,但是当我启动我的应用程序时它会立即强行关闭。我正在逐字地阅读一本安卓教程,但我仍然遇到错误。我的目标是在相对布局中创建列表视图。此外,由于某种原因,当我分配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会在不告诉我异常的情况下强行关闭。我很感激你的帮助。谢谢!
答案 0 :(得分:6)
您使用错误的Button
搜索id
。它应该是:
save = (Button)findViewById(R.id.button1);
另外,你对EditText
做同样的事情。像这样修改它:
EditText firstName = (EditText)findViewById(R.id.text1);
EditText lastName = (EditText)findViewById(R.id.text2);
您使用android.R.layout.some_id_here
的唯一时间是您使用定义的id
框架(例如,您将android.R.id.list
用于ListView
元素ListActivity
)中的布局。
答案 1 :(得分:5)
有点令人沮丧的是,android会在不告诉我异常的情况下强行关闭。
它就在日志中,它是java.lang.RuntimeException
并进一步说
引起:java.lang.NullPointerException
您正在使用findViewById
,其中 id 未在您的布局中定义。该函数找不到具有该id的任何视图并返回null。您稍后尝试取消引用此类null,从而解除NullPointerException
异常。
答案 2 :(得分:3)
android.R.id.button1
应为R.id.button1
答案 3 :(得分:3)
这是错误:
save = (Button)findViewById(android.R.id.button1);
你最好做那样的事情:
save = (Button)findViewById(R.id.button1);
导入正确的R文件:
import com.yournamespace.R;