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?
– Jorge B.
Log shows no error in any of my classes :\
– Caique Oliveira
Show all log please.
– Jorge B.
log complete: https://gist.github.com/jcaiqueoliveira/f25e212d7522e42a3297
– Caique Oliveira
And the xml of the layout?
– Jorge B.
XML Listview_layout: https://gist.github.com/jcaiqueoliveira/311c734c4d244781c5c9
– Caique Oliveira
The same list xml? layout_listview_quiz
– Jorge B.
This one https://gist.github.com/jcaiqueoliveira/9b014e56d0b6050d1f16
– Caique Oliveira
I don’t see where it’s from then.
– Jorge B.
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
– Caique Oliveira
What was the mistake?
– Jorge B.