ERROR findViewById Listview Fragment

Asked

Viewed 421 times

0

Hello, I have a problem that I can not solve, I created a list view but when "calling" in Java (has an arrow below) results in error when testing the application, this is the error:

08-04 19:59:25.765: D/skia(23307): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 270 
08-04 19:59:25.775: D/skia(23307): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 271 
08-04 19:59:25.805: I/PersonaManager(23307): getPersonaService() name persona_policy
08-04 19:59:25.845: I/PersonaManager(23307): getPersonaService() name persona_policy
08-04 19:59:25.855: D/skia(23307): GFXPNG PNG bitmap created width:48 height:48 bitmap id is 272 
08-04 19:59:25.875: E/MoreInfoHPW_ViewGroup(23307): Parent view is not a TextView
08-04 19:59:25.895: D/skia(23307): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 273 
08-04 19:59:25.895: I/PersonaManager(23307): getPersonaService() name persona_policy
08-04 19:59:26.125: D/AndroidRuntime(23307): Shutting down VM
08-04 19:59:26.145: E/AndroidRuntime(23307): FATAL EXCEPTION: main
08-04 19:59:26.145: E/AndroidRuntime(23307): Process: com.sapires.Pizza, PID: 23307
08-04 19:59:26.145: E/AndroidRuntime(23307): java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
08-04 19:59:26.145: E/AndroidRuntime(23307):    at com.sapires.Joes.Ementa.onCreateView(Ementa.java:46)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.View.measure(View.java:17478)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.View.measure(View.java:17478)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:412)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.View.measure(View.java:17478)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.View.measure(View.java:17478)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2262)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1391)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1590)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6585)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.Choreographer.doFrame(Choreographer.java:573)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.os.Handler.handleCallback(Handler.java:733)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.os.Looper.loop(Looper.java:136)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at android.app.ActivityThread.main(ActivityThread.java:5579)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at java.lang.reflect.Method.invoke(Native Method)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
08-04 19:59:26.145: E/AndroidRuntime(23307):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)

This is the Java:

    package com.sapires.Joes;

import android.support.v4.app.Fragment;
import com.sapires.Pizza.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Ementa extends Fragment {

    String[] titulos;
    String[] descricoes;
    int[] images = { R.drawable.um, R.drawable.dois, R.drawable.tres,
            R.drawable.quatro };

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

        View android = inflater.inflate(R.layout.ementa_2, container, false);

        Resources res = getResources();
        titulos = res.getStringArray(R.array.titulos);
        descricoes = res.getStringArray(R.array.descricoes);

    ->  ListView list = (ListView) getView().findViewById(R.id.listView1);

        return android;
    }

}

class VivzAdapter extends ArrayAdapter<String>{

    VivzAdapter(Context c, String[] titulos){
        super(c,R.layout.ementa_2, R.id.textView1, titulos);
    }

}

This is the layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#5B92E5" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="130dp"
        android:layout_height="96dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:src="@drawable/um" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView"
        android:layout_margin="10dp"
        android:layout_toRightOf="@+id/imageView"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:layout_toRightOf="@+id/imageView"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

1 answer

1


Have you inflated your view on onCreateView, but the method has not yet returned the inflated view. So the method getView() will return null, as there is no view to return. That’s why Nullpointerexception occurs. You, instead, can directly use the instance of your inflated view in onCreateView.

Replace the line:

ListView list = (ListView) getView().findViewById(R.id.listView1);

for:

ListView list = (ListView) android.findViewById(R.id.listView1);

Or you can use the method onViewCreated for that reason:

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
     ListView list = (ListView) view.findViewById(R.id.listView1);
}

I hope that solves the problem!

  • Decided, 5 stars, thank you very much, I realized why it didn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.