Textview.settext () providing a nullpointer exception

This is an action that invokes a text view and receives an intent.

package com.example.smartbrowser;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.widget.TextView;

public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView vw =(TextView) findViewById(R.id.text);

Intent intent =getIntent();

String message = intent.getStringExtra(Browseractivity.Message);


vw.setText(message);


setContentView(R.layout.activity_main);
    }


}

And activity_main.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=".MainActivity" >


 <TextView

  android:id="@+id/text"

  android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:layout_alignParentLeft="true"

  android:layout_alignParentTop="true"


  />


</RelativeLayout>

Why am I getting a null pointer exception in MainActivity.java?

+3
source share
4 answers

You must place setContentView(R.layout.activity_main);before initializing any kind. Here, your was initialized TextViewbefore setting the content, so it returned null.

@Override 
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView vw =(TextView) findViewById(R.id.text);

    Intent intent =getIntent();

    String message = intent.getStringExtra(Browseractivity.Message);
    vw.setText(message);
}
+3
source

Change to

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // should be first
TextView vw =(TextView) findViewById(R.id.text); // then initialize textview
Intent intent =getIntent();
String message = intent.getStringExtra(Browseractivity.Message);
vw.setText(message);
}

First you need to set the contents of the layout first and then initialize your views.

findViewById . NullPointerException bcoz, TextView, .

0

You should always set the content representation in this zircon state before playing with any widget as a text representation in your example

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView vw =(TextView) findViewById(R.id.text);

Intent intent =getIntent();

String message = intent.getStringExtra(Browseractivity.Message);


vw.setText(message);



    }


}

That should do the trick :) hope it helps

0
source

I know this is old, but I will answer it as this code is still applicable.

MainActivity.java

    public class MainActivity extends Activity {

private TextView textView2; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main); // Change from activity_main > fragment_main
    textView2 = (TextView)findViewById(R.id.textview2);
    textView2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View textView2) {
            // TODO Auto-generated method stub

            if (textView2.isClickable() == true){
                ((TextView) textView2).setText("Changed!!!!!!");                    
            }
        }
    });


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

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<!-- REMOVE android:id="@+id/container" -->
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.thinktankcoding.androiddevtest.MainActivity"
tools:ignore="MergeRootFrame" />

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container" <!-- ADD THE LINE YOU REMOVED HERE AND REMOVE THESE COMMENTS -->
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.thinktankcoding.androiddevtest.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/textview2"
    android:layout_width="fill_parent"
    android:lines="2"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textview1"
    android:layout_below="@+id/textview1"
    android:clickable="true" />

0
source

All Articles