NullPointer Exception when assigning value to Text

2019-04-16 04:43发布

问题:

I just wanted to passing value from one activity to another but not able to find Textview value from the view.. I just passing one message from main activity to this activity through a text box and I'm seeing textbox value here but not able to assign that to textView...

here is my code..

public class DisplayMessageActivity extends Activity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

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

    textView = (TextView) findViewById(R.id.displayText);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    textView.setText(message);


}

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.activityapps.DisplayMessageActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/displayText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView" />

Please help me out..

回答1:

The TextView does not belong to the Activity layout. Move the initialization of TextView to onCreateView of PlaceholderFragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
    TextView textView = (TextView) rootView.findViewById(R.id.displayText);
    return rootView;
}

In Activity

   if (savedInstanceState == null) {
   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
   Bundle bundle = new Bundle();
   bundle.putString("key", message);
   PlaceholderFragment fragobj = new   PlaceholderFragment();
   fragobj.setArguments(bundle);
        getFragmentManager().beginTransaction()
                .add(R.id.container,  fragobj).commit();
    }

Then

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.displayText);
        String strtext = getArguments().getString("key");
        textView.setText(strtext);
        return rootView;
    }


回答2:

View view = inflater.inflate(R.layout.fragment_display_message, container, false);
TextView textView = (TextView) view.findViewById(R.id.displayText);
return view; //in onCreate method

you have extended from activity you can use also this

 TextView textView = (TextView) findViewById(R.id.displayText);
  //in onCreate method