hitesh
hitesh

Reputation: 378

Textview.settext() giving nullpointer exception

This is activity which is calling textview and getting the 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 I am getting null pointer exception in MainActivity.java?

Upvotes: 3

Views: 3649

Answers (4)

codelinx
codelinx

Reputation: 125

I know this is old but i will answer it since 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" />

Upvotes: 0

Hamid Shatu
Hamid Shatu

Reputation: 9700

You have to place setContentView(R.layout.activity_main); before initializing any view. Here, initialized your TextView before setting your content view thats why it was returning 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);
}

Upvotes: 4

Raghunandan
Raghunandan

Reputation: 133560

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);
}

You need to set the content of the layout to the activity first and then initialize your views.

findViewById looks for a view in the current inflated layout. You got NullPointerException bcoz you initialized TextView before setting the layout to the activity.

Upvotes: 0

murielK
murielK

Reputation: 1020

You should always in this circonstance set your content view before playing with any widget as a Textview 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);



    }


}

This should do the trick :) hope it helped

Upvotes: 0

Related Questions