我可以使用DataOutputActivity.java中的System.out.println()打印变量(latitude、longitude、imageloc)的值,但当我尝试将文本视图的文本设置为上述变量之一时,应用程序会关闭。我还在布局文件中声明了文本视图的ID。我无法弄清楚问题出在哪里。没有语法错误,只有运行时错误。如果我删除onCreate()方法的最后两行,则没有问题。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_ouput);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String latitude = intent.getStringExtra(HomeActivity.EXTRA_LATITUDE);
String longitude = intent.getStringExtra(HomeActivity.EXTRA_LONGITUDE);
String imageLoc = intent.getStringExtra(HomeActivity.EXTRA_IMAGELOC);
TextView textview = (TextView) findViewById(R.id.outputText);
textview.setText(latitude);
setContentView(textview);
}
fragment_data_output.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="com.example.urbsalus.DataOuputActivity$PlaceholderFragment" >
<TextView
android:id="@+id/outputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Latitude"
/>
</RelativeLayout>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title_activity_data_ouput">DataOuputActivity</string>
<string name="Latitude">Latitude</string>
</resources>
LogChat
07-22 17:04:18.761: D/OpenGLRenderer(15827): Flushing caches (mode 0)
07-22 17:04:19.231: D/OpenGLRenderer(15827): Flushing caches (mode 2)
07-22 17:04:19.651: D/OpenGLRenderer(15827): Flushing caches (mode 0)
07-22 17:04:20.201: D/memalloc(15827): /dev/pmem: Unmapping buffer base:0x51fb1000 size:6144000 offset:4608000
07-22 17:04:20.211: D/memalloc(15827): /dev/pmem: Unmapping buffer base:0x525f3000 size:7680000 offset:6144000
07-22 17:04:20.321: D/CLIPBOARD(15827): Hide Clipboard dialog at Starting input: finished by someone else... !
07-22 17:04:20.341: W/IInputConnectionWrapper(15827): showStatusIcon on inactive InputConnection
07-22 17:04:30.181: D/AndroidRuntime(15827): Shutting down VM
07-22 17:04:30.181: W/dalvikvm(15827): threadid=1: thread exiting with uncaught exception (group=0x40c0da68)
07-22 17:04:30.191: E/AndroidRuntime(15827): FATAL EXCEPTION: main
07-22 17:04:30.191: E/AndroidRuntime(15827): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.urbsalus/com.example.urbsalus.DataOuputActivity}: java.lang.NullPointerException
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.os.Looper.loop(Looper.java:137)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-22 17:04:30.191: E/AndroidRuntime(15827): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 17:04:30.191: E/AndroidRuntime(15827): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 17:04:30.191: E/AndroidRuntime(15827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-22 17:04:30.191: E/AndroidRuntime(15827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-22 17:04:30.191: E/AndroidRuntime(15827): at dalvik.system.NativeStart.main(Native Method)
07-22 17:04:30.191: E/AndroidRuntime(15827): Caused by: java.lang.NullPointerException
07-22 17:04:30.191: E/AndroidRuntime(15827): at com.example.urbsalus.DataOuputActivity.onCreate(DataOuputActivity.java:30)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.Activity.performCreate(Activity.java:4470)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-22 17:04:30.191: E/AndroidRuntime(15827): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-22 17:04:30.191: E/AndroidRuntime(15827): ... 11 more
07-22 17:04:38.141: I/Process(15827): Sending signal. PID: 15827 SIG: 9
com.example.urbsalus.DataOuputActivity.onCreate(DataOuputActivity.java:30)
。 - i.n.e.ftextview.setText(latitude).tostring();
- prabhakaran