This is a simple activity which takes the message from user and displays it in the second activity. From nowhere I am getting a java.lang Null pointer exception.
The data in the file is as follows :
// activity_main.xml // 这是主要活动的 xml 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="29dp"
android:text="Enter the message"
android:textSize="18dp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="38dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="send"
android:onClick="sendmessage" />
</RelativeLayout>
// MainActivity.java // 这是主活动的代码,它创建一个意图并将输入的值传递给显示活动
package com.example.simpleactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.simpleactivity.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendmessage(View view) {
EditText textmessage = (EditText) findViewById(R.id.editText1);
String message = textmessage.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@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;
}
}
// activity_display_message.xml // 这是显示活动的 xml 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:layout_marginTop="110dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
// DisplayMessageActivity.java // 这是获取和意图并显示额外消息的显示活动
package com.example.simpleactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
tv = (TextView) findViewById(R.id.textView2);
tv.setText(message);
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
}
// logcat如下:
08-09 18:23:08.812: I/Choreographer(1520): Skipped 37 frames! The application may be doing too much work on its main thread.
08-09 18:23:08.942: D/gralloc_goldfish(1520): Emulator without GPU emulation detected.
08-09 18:23:13.262: D/AndroidRuntime(1520): Shutting down VM
08-09 18:23:13.262: W/dalvikvm(1520): threadid=1: thread exiting with uncaught exception
(group=0x414c4700)
08-09 18:23:13.402: E/AndroidRuntime(1520): FATAL EXCEPTION: main
08-09 18:23:13.402: E/AndroidRuntime(1520): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpleactivity/com.example.simpleactivity.DisplayMessageActivity}: java.lang.NullPointerException
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.os.Looper.loop(Looper.java:137)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-09 18:23:13.402: E/AndroidRuntime(1520): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 18:23:13.402: E/AndroidRuntime(1520): at java.lang.reflect.Method.invoke(Method.java:525)
08-09 18:23:13.402: E/AndroidRuntime(1520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-09 18:23:13.402: E/AndroidRuntime(1520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-09 18:23:13.402: E/AndroidRuntime(1520): at dalvik.system.NativeStart.main(Native Method)
08-09 18:23:13.402: E/AndroidRuntime(1520): Caused by: java.lang.NullPointerException
08-09 18:23:13.402: E/AndroidRuntime(1520): at com.example.simpleactivity.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:20)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.Activity.performCreate(Activity.java:5133)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-09 18:23:13.402: E/AndroidRuntime(1520): ... 11 more
08-09 18:23:17.812: I/Process(1520): Sending signal. PID: 1520 SIG: 9