Caused by: java.lang.Unsupportedoperationexception: addView(View) is not supported in Adapterview

Asked

Viewed 92 times

1

I’m making that mistake:

Caused by: java.lang.Unsupportedoperationexception: addView(View) is not supported in Adapterview

I researched and the errors that were cited did not solve my problem.

I don’t understand this problem.

Code of my Adapter:

public class ListaQuizAdapter extends ArrayAdapter<String> {
    private ArrayList<Quiz> listaQuiz;
    private MainActivity mainActivity;
    private Context context;
    public ListaQuizAdapter(Context context, ArrayList<Quiz> listaQuiz,MainActivity mainActivity) {
        super(context, R.layout.layout_listview_quiz);
        this.listaQuiz = listaQuiz;
        this.mainActivity = mainActivity;
        this.context = context;
    }

    @Override
    public int getCount() {
        return listaQuiz.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder v;
        try {
            if (convertView == null) {
                v = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.layout_listview_quiz, parent,false);
                v.nome = (TextView) convertView.findViewById(R.id.nomeQuiz);
                v.numero = (TextView) convertView.findViewById(R.id.numeroQuestoes);
                v.nome.setText(listaQuiz.get(position).getNome());
                v.numero.setText(listaQuiz.get(position).getNumeroQuestoes());
                v.iniciar = (Button) convertView.findViewById(R.id.iniciar);
                v.iniciar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mainActivity.getSupportFragmentManager().beginTransaction().replace(R.id.container, new ResponderQuizFragment()).addToBackStack(null).commit();
                    }
                });
                convertView.setTag(v);
            } else {
                v = (ViewHolder) convertView.getTag();
            }
        }catch (Exception e){
            e.printStackTrace();
            //Log.e("Exception", e.toString());
        }
        return convertView;
    }

    public class ViewHolder {
        TextView nome, numero;
        Button iniciar;
    }
}

Mainactivity code

public class MainActivity extends ActionBarActivity {
    public Toolbar actionToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.container, new QuizFragment()).commit();
        }
    }
}

Quiz Fragment:

public class QuizFragment extends android.support.v4.app.Fragment {
    @Bind(R.id.lista_quiz)
    ListView listaQuiz;
    private MainActivity mainActivity;

    public QuizFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_quiz, container, false);
        ButterKnife.bind(this, view);
        mainActivity = (MainActivity) getActivity();
        mainActivity.actionToolbar.setTitle("Quiz");
        Quiz q = new Quiz();
        q.setNome("Rebimboca");
        q.setNumeroQuestoes("18Questoes");
        ArrayList<Quiz> quiz = new ArrayList<>();
        quiz.add(q);
        ListaQuizAdapter listaQuizAdapter = new ListaQuizAdapter(getActivity(), quiz, mainActivity);
        listaQuiz.setAdapter(listaQuizAdapter);
        listaQuiz.setFocusable(false);
        return view;
    }
}

XML by Listview:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="br.net.getinfo.treinamentos.fragment.QuizFragment">

    <ListView
        android:id="@+id/lista_quiz"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Any idea of this mistake?

  • Which line of error?

  • Log shows no error in any of my classes :\

  • Show all log please.

  • log complete: https://gist.github.com/jcaiqueoliveira/f25e212d7522e42a3297

  • And the xml of the layout?

  • XML Listview_layout: https://gist.github.com/jcaiqueoliveira/311c734c4d244781c5c9

  • The same list xml? layout_listview_quiz

  • This one https://gist.github.com/jcaiqueoliveira/9b014e56d0b6050d1f16

  • I don’t see where it’s from then.

  • I found the error, it was lack of attention on my part :| The error was that instead of creating a container using Linearlayout I made use of the <Listview> tag when I tried to replace the finished Fragment generating that exception

  • What was the mistake?

Show 6 more comments

1 answer

0


I found the mistake. The error was that instead of creating a container with using Linearlayout I made use of the tag when I tried to replace the finished Fragment generating the execution: unsupportedoperationexception-addviewview-is-not-support

Browser other questions tagged

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