Domanda

when i run my app i get this error in the log-cat:

Caused by: java.lang.NullPointerException
at com.myfirstapp.myfirstapp.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
... 11 more

Here's line 52:

String message = editTextInput.getText().toString();

So far i've worked out that the NPE must be when I define the EditText (or any view for that matter):

EditText editText = (EditText) findViewById(R.id.input_text);

However though, I don't get an NPE when I define a EditText without: findViewById() like this View:

TextView desc = new TextView(this);

Here is the entire onCreate() method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    EditText editText = (EditText) findViewById(R.id.input_text);
    Button submit = (Button) findViewById(R.id.submit);
    LinearLayout layout = (LinearLayout) findViewById(R.id.layout_main);
    Intent intent = new Intent(this, testActivity.class);
    final TextView desc = new TextView(this);
    final TextView title = new TextView(this);

    String message = editText.getText().toString();

    submit.setVisibility(View.VISIBLE);
    submit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            editText.setVisibility(View.GONE);}});

    desc.setTextSize(20);
    desc.setText(message);
    desc.setGravity(Gravity.LEFT | Gravity.TOP);
    desc.setPadding(5, 5, 5, 5);
    desc.setTextColor(getResources().getColor(R.color.black));

    title.setTextSize(10);
    title.setText(R.string.title_activity_dictionary);
    title.setGravity(Gravity.LEFT | Gravity.TOP);
    title.setPadding(5, 5, 5, 5);
    title.setTextColor(getResources().getColor(R.color.black));

    layout.addView(desc);
    layout.addView(title);
}

What I dont get is why is there a NPE when I clearly define the View?

È stato utile?

Soluzione 2

This is because findViewById() searches in the activity_main layout, while the button is located in the fragment's layout fragment_main.

Move that piece of code in the onCreateView() method of the fragment:

//...

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick = (Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onButtonClick((Button) view);
    }
});

Notice that now you access it through rootView view:

Button buttonClick = (Button)rootView.findViewById(R.id.button);

otherwise you would get again NullPointerException.

Altri suggerimenti

Make sure that you are passing the correct id in findviewbyid method which is available in your layout..

Otherwise. Just clean and build the project.

Change

String message = editTextInput.getText().toString();

To

String message = editText.getText().toString();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top