简体   繁体   中英

I cannot change the text in textview

I can print the values of variables (latitude, longitude, imageloc) using System.out.println() in DataOutputActivity.java, but when I try to set the text of textview with one of the above variable, the app shutdown. I have also declared the id for the textview in layout file. I cannot figure out what is wrong. There is no syntax error, only runtime error. If I remove the last two line of the onCreate() method, then there is no issue.

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

Firstly, remove this line, it is incorrect.

setContentView(textview);

Secondly in this line

String latitude = intent.getStringExtra(HomeActivity.EXTRA_LATITUDE);

you are trying to get a string form the Extras in the Intent (you are not reading from your Resources file). If the value in HomeActivity.EXTRA_LATITUDE doesn't exists, the string will be returned as null, hence your null pointer error.

you could check against this by doing something like

if (latitude != null) {
    textview.setText(latitude);
}

If the line 30 in onCreate is textView.settext(""); you must change the

setContenView(R.layout.activity_data_output);

to

setContentView(R.layout.fragment_data_output);

and do not call setContentView after the settext twice!!

That's because the TextView is in the Fragment. You're mixing the content view of the Activity with the displayed Fragment, and most likely the only thing created by setContentView(R.layout.activity_data_ouput) is a container for the PlaceholderFragment.

You should use the template for fragments specified in the following question: https://stackoverflow.com/questions/24840509/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default-simple-fr

你在LogCat中看到Caused by: java.lang.NullPointerException ,检查你没有new或没有引用对象的位置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM