Context error in Fragment

Asked

Viewed 421 times

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?

1 answer

1

The builder of VivzAdapter asks for a Context.

When you create the instance this way:

VivzAdapter adapter = new VivzAdapter(this, titulos, images, descricoes);  

this is a Context when Menu extends Activity, because Activity extends of Context. When Menu extends of Fragment this is a Fragment other than extends of Context, hence give error.

You can get a Context in a Fragment using the method getActivity(). Yet there are times when getActivity() can return null.

The best way to have a valid Context is to get it in the method onAttach of Fragment:

//Declara uma variável para guardar o context.
private Context context;

@Override
public void onAttach(Activity activity) {
    context = activity;
    super.onAttach(activity);
}

Use the variable context when you need a Context:

VivzAdapter adapter = new VivzAdapter(context, titulos, images, descricoes);
  • error is what is in the question below

  • 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)

  • 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

  • I could figure it out, just change: @Override public void onAttach(Activity Activity) { super.onAttach(Activity); context = Activity; }

  • I’m sorry. I was focused on the issue of Context I forgot to put the call to the base class method.

Browser other questions tagged

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