try traducir the que pointer method catch java android nullpointerexception android-arrayadapter

java - traducir - nullpointerexception processing



NullPointerException en CustomArrayAdapter (2)

Cambie el método getView siguiente manera

te perdiste para asignar convertView

@Override public View getView(int position, View convertView,ViewGroup parent){ PostHolder holder; LayoutInflater li = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if(convertView == null){ convertView = li.inflate(layoutid,parent,false); holder = new PostHolder(); holder.imgicon = (ImageView)convertView.findViewById(singlerowimgid); holder.title =(TextView)convertView.findViewById(R.id.txtTitle); } else { holder = (PostHolder)convertView.getTag(); } Post post = data[position]; holder.title.setText(post.title); holder.imgicon.setImageResource(post.imgicon); return convertView; } }

Intento usar un Adaptador de matriz personalizado para llenar un ListView en un Fragment . Pero findViewById() NullPointerException en el método findViewById() en el método findViewById() en Custom Array Adapter.

Aquí está el CustomAdapter.java:

package graph.prathya.com.materialdesign; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; /** * Created by Prathya on 5/19/2015. */ public class CustomAdapter extends ArrayAdapter<Post>{ Context context; int layoutid,singlerowimgid= R.id.imgIcon; Post data[]=null; public CustomAdapter(Context context, int layoutid, Post data[]){ super(context, layoutid); this.layoutid = layoutid; this.data = data; this.context =context; } private class PostHolder{ ImageView imgicon; TextView title; } @Override public int getCount(){ return data.length; } @Override public View getView(int position, View convertView,ViewGroup parent){ PostHolder holder; LayoutInflater li = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if(convertView == null){ li.inflate(layoutid,parent,false); holder = new PostHolder(); holder.imgicon = (ImageView)convertView.findViewById(singlerowimgid); holder.title =(TextView)convertView.findViewById(R.id.txtTitle); } else { holder = (PostHolder)convertView.getTag(); } Post post = data[position]; holder.title.setText(post.title); holder.imgicon.setImageResource(post.imgicon); return convertView; } }

Aquí está mi archivo XML que define la apariencia de una sola fila en ListFragment singlerow.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <ImageView android:id="@+id/imgIcon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_marginRight="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" /> <TextView android:id="@+id/txtTitle" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:textStyle="bold" android:textSize="22dp" android:textColor="#000000" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" />

Aquí está mi clase Fragmento: Fragment1.java:

package graph.prathya.com.materialdesign; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; /** * Created by Prathya on 5/19/2015. */ public class Fragment1 extends ListFragment{ Post post[] = new Post[]{ new Post(R.drawable.passion,"Passion"), new Post(R.drawable.event,"Event"), new Post(R.drawable.project,"Project"), new Post(R.drawable.groupstudy,"Group Study")}; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1,container,false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new CustomAdapter(getActivity(), R.layout.singlerow, post)); } }

Aquí está fragment1.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffaa00"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/list" android:layout_gravity="center_horizontal" />

Aquí está Post.java

package graph.prathya.com.materialdesign; /** * Created by Prathya on 5/19/2015. */ public class Post { public int imgicon; public String title; public Post(){ super(); } public Post(int imgicon, String title){ this.imgicon = imgicon; this.title = title; } }

Aquí está MainActivity.java

package graph.prathya.com.materialdesign; import android.app.FragmentTransaction; import android.support.v4.app.FragmentManager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addframent(); } public void addframent(){ FragmentManager fragmentManager = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment1 fm1 = new Fragment1(); fragmentTransaction.add(R.id.sa,fm1); fragmentTransaction.commit(); } }

y aquí está el mensaje de error

05-19 19: 26: 13.362 18306-18306 / graph.prathya.com.materialdesign E / AndroidRuntime: EXCEPCIÓN FATAL: proceso principal: graph.prathya.com.materialdesign, PID: 18306 java.lang.NullPointerException: intento de invocación virtual Método ''android.view.View android.view.View.findViewById (int)'' en una referencia de objeto nulo en graph.prathya.com.materialdesign.CustomAdapter.getView (CustomAdapter.java:42) en android.widget.AbsListView.obtainView (AbsListView.java:2347) en android.widget.ListView.measureHeightOfChildren (ListView.java:1270) en android.widget.ListView.onMeasure (ListView.java:1182) en android.view.View.measure (View.java: 17554)

..


En su CustomAdapter.java tiene que asignarle un diseño inflado a convertView :

convertView = li.inflate(layoutid,parent,false);

Actualización: debe configurar el titular para su vista recién inflada:

covnertView.setTag(holder);