Error with Updating Text View from java file

不羁的心 提交于 2020-01-11 14:41:28

问题


Sorry this may be a simple error, new to android dev, but I've been searching the web looking for answers and none of them solutions seem to work. Every time I try run the code below it fails, any ideas why/how to fix it. First is the java file

public class Services extends Activity {
Report2 r;  
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle data = getIntent().getExtras();

    r = data.getParcelable("RemarksReport");

    setContentView(R.layout.activity_services);

    TextView sampleText = (TextView) findViewById(R.id.services_text);

works until here, then crashes at the next line

    sampleText.setText("ANYTHING");

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

The part in the XML file looks like this,

        <TextView
        android:id="@+id/services_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="42px"
        android:text="@string/sample_services" />

where this is within a table, in a linear layout.

Logcat Errors :

07-01 22:45:09.952: E/AndroidRuntime(8854): FATAL EXCEPTION: main
07-01 22:45:09.952: E/AndroidRuntime(8854): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jasc/com.example.jasc.Services}: java.lang.NullPointerException
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.os.Looper.loop(Looper.java:137)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.ActivityThread.main(ActivityThread.java:4441)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at java.lang.reflect.Method.invoke(Method.java:511)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at dalvik.system.NativeStart.main(Native Method)
07-01 22:45:09.952: E/AndroidRuntime(8854): Caused by: java.lang.NullPointerException
07-01 22:45:09.952: E/AndroidRuntime(8854):     at com.example.jasc.Services.onCreate(Services.java:30)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.Activity.performCreate(Activity.java:4465)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-01 22:45:09.952: E/AndroidRuntime(8854):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)

回答1:


Inside your Fragment's onCreateView change this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.place_holder_fragment, container, false);
    servicesTxtView= (TextView) view.findViewById(R.id.services_text);
    servicesTxtView.setText("ANYTHING");
    return view;
}

You are trying to initialize textview without using its container view. So you are getting Null Pointer Exception while you call setText on it.




回答2:


Looks like that this TextView is not a part of activity_services.xml, but rather a part of view you provide for PlaceholderFragment, I suppose. If so, you should work with it by using PlaceholderFragment's view (getView) as root view for findViewById method




回答3:


You just need to write this code

 Bundle data = getIntent().getExtras();

 r = data.getParcelable("RemarksReport");

after this

 setContentView(R.layout.activity_services);

So final code

  setContentView(R.layout.activity_services);
  Bundle data = getIntent().getExtras();
  r = data.getParcelable("RemarksReport");


来源:https://stackoverflow.com/questions/24508303/error-with-updating-text-view-from-java-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!