请帮助我。我是android.i的初学者有一个Android应用程序。 我有一个buttom,编辑文本,我想要我用文字写的每一件事,发送到putextra的另一个页面并在textView中显示它。但是当我点击这个buttom我app停止并说:sorry.application意外停止。请再试一次。谢谢你的关注。 这是log cat错误:
08-16 17:17:30.001: E/AndroidRuntime(736): FATAL EXCEPTION: main
08-16 17:17:30.001: E/AndroidRuntime(736): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapplicationandroid/com.example.secondapplicationandroid.dovomi}: java.lang.NullPointerException
08-16 17:17:30.001: E/AndroidRuntime(736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.os.Looper.loop(Looper.java:123)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 17:17:30.001: E/AndroidRuntime(736): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:17:30.001: E/AndroidRuntime(736): at java.lang.reflect.Method.invoke(Method.java:507)
08-16 17:17:30.001: E/AndroidRuntime(736): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 17:17:30.001: E/AndroidRuntime(736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 17:17:30.001: E/AndroidRuntime(736): at dalvik.system.NativeStart.main(Native Method)
08-16 17:17:30.001: E/AndroidRuntime(736): Caused by: java.lang.NullPointerException
08-16 17:17:30.001: E/AndroidRuntime(736): at com.example.secondapplicationandroid.dovomi.onCreate(dovomi.java:14)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:17:30.001: E/AndroidRuntime(736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-16 17:17:30.001: E/AndroidRuntime(736): ... 11 more
08-16 17:17:33.012: I/Process(736): Sending signal. PID: 736 SIG: 9
08-16 17:21:48.723: W/KeyCharacterMap(778): No keyboard for id 0
08-16 17:21:48.723: W/KeyCharacterMap(778): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-16 17:21:49.891: D/AndroidRuntime(778): Shutting down VM
08-16 17:21:49.891: W/dalvikvm(778): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-16 17:21:49.921: E/AndroidRuntime(778): FATAL EXCEPTION: main
08-16 17:21:49.921: E/AndroidRuntime(778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapplicationandroid/com.example.secondapplicationandroid.dovomi}: java.lang.NullPointerException
08-16 17:21:49.921: E/AndroidRuntime(778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.os.Looper.loop(Looper.java:123)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 17:21:49.921: E/AndroidRuntime(778): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:21:49.921: E/AndroidRuntime(778): at java.lang.reflect.Method.invoke(Method.java:507)
08-16 17:21:49.921: E/AndroidRuntime(778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 17:21:49.921: E/AndroidRuntime(778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 17:21:49.921: E/AndroidRuntime(778): at dalvik.system.NativeStart.main(Native Method)
08-16 17:21:49.921: E/AndroidRuntime(778): Caused by: java.lang.NullPointerException
08-16 17:21:49.921: E/AndroidRuntime(778): at com.example.secondapplicationandroid.dovomi.onCreate(dovomi.java:21)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:21:49.921: E/AndroidRuntime(778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-16 17:21:49.921: E/AndroidRuntime(778): ... 11 more
08-16 17:26:50.131: I/Process(778): Sending signal. PID: 778 SIG: 9
确定。 我的MainActivity.java是:
package com.example.secondapplicationandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b= (Button) findViewById(R.id.button2);
final EditText matn = (EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s=matn.getText().toString();
Intent mafhoom=new Intent(MainActivity.this, dovomi.class);
mafhoom.putExtra("varedshode", s);
startActivity(mafhoom);
//startActivity(new Intent(MainActivity.this, dovomi.class));
}//end onclick
});
}//end void onCreate
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and my second page that named is,dovomi.java is:
package com.example.secondapplicationandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class dovomi extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView matni=(TextView) findViewById(R.id.textView2);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String j =(String) b.get("varedshode");
matni.setText(j);
}
//matni.setText(getIntent().getExtras().getString("varedshode"));
setContentView(R.layout.dovomi);
}
}
,清单是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.secondapplicationandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.secondapplicationandroid.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".dovomi"></activity>
</application>
</manifest>
和for layer,dovomi.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/salam" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
答案 0 :(得分:1)
问题是行TextView matni=(TextView) findViewById(R.id.textView2);
在调用findViewById
而未指定视图的活动中,检查当前活动的setContentView
视图。问题是您在致电setContentView(R.layout.dovomi);
后致电findviewbyid
。
请确保在任何基本setContentView()
来电之前致电findViewById
。
所以在你的第二节课中,只需将setContentView(R.layout.dovomi);
放在super.onCreate(savedInstanceState);