androidandroid-intentandroid-activity

NullPointer Exception on FindViewById for TextView using Android


When I try to change the textview(detail_text1) using an intent I get a NullPointerException.

The reason for this exception is, that the findViewById call returns null.

I already tried setting the context before but it still gives the same exception.

Please see below:

Detail Activity

public class DetailActivity extends Activity {
...
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle 
                                                        savedInstanceState){

           //Activity called via intent. Inspect for forecast data.
           Intent rcvIntent = getActivity().getIntent();
           //TextView text = (TextView) view.findViewById(R.id.detail_text1);
           View rootView = inflater.inflate(R.layout.fragment_detail,container
                                                                        ,false);

                if(rcvIntent != null && rcvIntent.hasExtra(Intent.EXTRA_TEXT)){
                    String forecastStr = rcvIntent
                                         .getStringExtra(Intent.EXTRA_TEXT);

                  //View myView = (View) view.getParent();
                  //setContentView(R.layout.fragment_detail);
        //ERROR HERE!
                   TextView tv = (TextView)rootView
                                           .findViewById(R.id.detail_text1);
                    tv.setText(forecastStr);
                }

                return rootView;
            }
}

Fragment Detail

<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"
    tools:context="com.example.rv.myapplication
                          .DetailActivity.DetailFragment">

    <TextView
        android:text="@+id/detail_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Activity Detail

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.rv.myapplication.DetailActivity"
    tools:ignore="MergeRootFrame" />

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rv.myapplication" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".DetailActivity"
            android:label="@string/title_activity_detail"
            android:parentActivityName=".MyActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.rv.myapplication.MyActivity" />
        </activity>
    </application>

</manifest>

Let me know it there is more code needed.

Thanks all!

Imports by Request

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

Solution

  • you problem is here:

    in Activity lifecycle onCreateView is:

    public View onCreateView (View parent, String name, Context context, AttributeSet attrs)
    

    and

    onCreateView(String name, Context context, AttributeSet attrs)
    

    but in fragment lifecycle onCreatView is

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

    so you mess up those.

    so what you should do?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_detail);  
    
               Intent rcvIntent = getActivity().getIntent();
    
                    if(rcvIntent != null && rcvIntent.hasExtra(Intent.EXTRA_TEXT)){
                        String forecastStr = rcvIntent
                                             .getStringExtra(Intent.EXTRA_TEXT);
    
           TextView tv = (TextView)findViewById(R.id.detail_text1);                                       
                        tv.setText(forecastStr);
                    }
    
                }