0
have a problem, I found a tutorial to create a Listview but is implemented with extends Activity
and I’m with extend Fragment
and so I have an error in the place marked with an arrow:
package com.sapires.Joes;
import android.support.v4.app.Fragment;
import com.google.android.gms.internal.im;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Ementa extends Fragment {
Context cont;
String[] titulos;
String[] descricoes;
int[] images = { R.drawable.um, R.drawable.dois, R.drawable.tres,
R.drawable.quatro,R.drawable.cinco,R.drawable.seis };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(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) android.findViewById(R.id.listView1);
--> VivzAdapter adapter = new VivzAdapter(this, titulos, images, descricoes);
list.setAdapter(adapter);
return android;
}
}
class VivzAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
VivzAdapter(Context c,String[] titulos, int imgs[], String[] desc) {
super(c, R.layout.ementa_2, R.id.textView1, titulos);
this.context = c;
this.images = imgs;
this.titleArray = titulos;
this.descriptionArray = desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.ementa_2, parent, false);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
TextView myTitle = (TextView) row.findViewById(R.id.textView1);
TextView myDescription = (TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
return row;
}
}
the mistake is: The constructor VivzAdapter(Ementa, String[], int[], String[]) is undefined
But changing to extend Activity
works, can help?
error is what is in the question below
– SaPires
Now make this mistake:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.sapires.Joes.Ementa.onCreateView(Ementa.java:52)
– SaPires
I reviewed the text and everything is OK :S listview exists if you want to take a look: https://www.dropbox.com/s/rqkvw6qaftyswy6/SwipeView.rar
– SaPires
I could figure it out, just change: @Override public void onAttach(Activity Activity) { super.onAttach(Activity); context = Activity; }
– SaPires
I’m sorry. I was focused on the issue of
Context
I forgot to put the call to the base class method.– ramaral