我试图在一个Activity中显示一个listview,但是当我尝试从一个从操作栏选项卡调用的片段中显示相同的listview时,我的程序没有运行。我曾尝试使用Fragment和ListFragment,但即使我在Internet上找到的一些示例也没有用。任何人都可以帮助我吗?
当我尝试使用操作栏启动活动时,我得到的错误以此开头:
01-09 03:10:37.807:E / AndroidRuntime(1248):致命异常:主要 01-09 03:10:37.807:E / AndroidRuntime(1248):java.lang.RuntimeException:无法启动活动ComponentInfo {com.app/com.app.TelaPrincipal}:java.lang.RuntimeException:您的内容必须具有ListView的id属性为'android.R.id.list'
那么,我需要在TelaPrincipal.java上创建任何List吗?
TelaPrincipal.java
package com.app;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class TelaPrincipal extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_principal);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(true);
//setupTabs();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1 = ab.newTab();
tab1.setTag("Professores");
tab1.setText("Professores");
tab1.setIcon(R.drawable.ac_professores);
tab1.setTabListener(new NavegacaoTabs(new FListaProfessores()));
ab.addTab(tab1);
//Adicionar outras Tabs depois
}
private class NavegacaoTabs implements TabListener {
private Fragment frag;
public NavegacaoTabs(Fragment frag){
this.frag = frag;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.i("Script", "onTabReselected()");
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
getSupportActionBar().setTitle(tab.getTag().toString());
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
fts.replace(R.id.fragmentcontainer, frag);
fts.commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
fts.remove(frag);
fts.commit();
}
}
}
tela_principal.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/fragmentcontainer"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Professor.java
package com.app;
public class Professor {
private String nome;
private String disciplina;
private int avaliacoes;
private int aprovacao;
private int reprovacao;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getDisciplina() {
return disciplina;
}
public void setDisciplina(String disciplina) {
this.disciplina = disciplina;
}
public int getAvaliacoes() {
return avaliacoes;
}
public void setAvaliacoes(int avaliacoes) {
this.avaliacoes = avaliacoes;
}
public int getAprovacao() {
return aprovacao;
}
public void setAprovacao(int aprovacao) {
this.aprovacao = aprovacao;
}
public int getReprovacao() {
return reprovacao;
}
public void setReprovacao(int reprovacao) {
this.reprovacao = reprovacao;
}
}
professor.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="10">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/vermelho_linha">
</View>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:layout_weight="89">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/professor_tvNome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:layout_centerInParent="true"
android:textSize="28sp"
android:textColor="@color/preto"
android:text="TextView" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/professor_tvDisciplina"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:layout_centerInParent="true"
android:textSize="15sp"
android:ellipsize="end"
android:textColor="@color/preto"
android:text="TextView" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/professor_tvAvalicaoes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_centerInParent="true"
android:textColor="@color/preto"
android:text="TextView" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/professor_tvAprovacoes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="15sp"
android:textColor="@color/verde"
android:text="TextView" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
ProfessorAdapter.java
package com.app;
import java.lang.reflect.Array;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ProfessorAdapter extends BaseAdapter {
private Context context;
private ArrayList<Professor> lista;
public ProfessorAdapter(Context context, ArrayList<Professor> lista){
this.context = context;
this.lista = lista;
}
@Override
public int getCount() {
return lista.size();
}
@Override
public Object getItem(int position) {
return lista.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Professor professor = lista.get(position);
View layout;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.professor, null);
}
else{
layout = convertView;
}
TextView nomeProfessor = (TextView) layout.findViewById(R.id.professor_tvNome);
nomeProfessor.setText(professor.getNome());
TextView avaliacoesProfessor = (TextView) layout.findViewById(R.id.professor_tvAvalicaoes);
avaliacoesProfessor.setText(professor.getAvaliacoes() + " avaliações");
TextView disciplinaProfessor = (TextView) layout.findViewById(R.id.professor_tvDisciplina);
disciplinaProfessor.setText(professor.getDisciplina());
float porcentagemAprovacao = ( (float) professor.getAprovacao() / (float) professor.getAvaliacoes() ) *100;
TextView aprovacoesProfessor = (TextView) layout.findViewById(R.id.professor_tvAprovacoes);
if(porcentagemAprovacao >50){
aprovacoesProfessor.setTextColor(Color.parseColor("#00FF00"));
}
else{
aprovacoesProfessor.setTextColor(Color.parseColor("#FF0000"));
}
String porcento = String.format("%.1f", porcentagemAprovacao);
aprovacoesProfessor.setText(porcento + "% aprovações");
return layout;
}
}
FListaProfessores.java&lt;&lt;我认为这个错误出现在这个文件中,我做了很多改动,尝试了一切都没有成功,这是我做的最后一次尝试。
package com.app;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FListaProfessores extends ListFragment {
ProfessorAdapter adapter;
private static ArrayList<Professor> professores = new ArrayList<Professor>();
private static ListView lv;
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
fillProfessores(professores);
adapter = new ProfessorAdapter(getActivity(), professores);
setListAdapter(adapter);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Professor temp = (Professor) parent.getItemAtPosition(position);
String nomeProfessor = (String) temp.getNome();
Toast.makeText(getView().getContext(), "Professor " + nomeProfessor + " clicado", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.flista_professores, null, false);
return (view);
}
public void fillProfessores(ArrayList<Professor> professores){
Professor professor = new Professor();
professor.setNome("Professor 1");
professor.setDisciplina("Materia 1");
professor.setAvaliacoes(125);
professor.setAprovacao(83);
professores.add(professor);
Professor professor2 = new Professor();
professor2.setNome("Professor 2");
professor2.setDisciplina("Materia 2");
professor2.setAvaliacoes(233);
professor2.setAprovacao(182);
professores.add(professor2);
}
}
flista_professres.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lista_professores"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:divider="@color/azul_linha"
android:dividerHeight="1dp"
style="@style/DropDownListView.Cadernotema">
</ListView>
</LinearLayout>
LogCat消息
01-09 03:10:37.807: E/AndroidRuntime(1248): FATAL EXCEPTION: main
01-09 03:10:37.807: E/AndroidRuntime(1248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.TelaPrincipal}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.os.Looper.loop(Looper.java:137)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 03:10:37.807: E/AndroidRuntime(1248): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 03:10:37.807: E/AndroidRuntime(1248): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 03:10:37.807: E/AndroidRuntime(1248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 03:10:37.807: E/AndroidRuntime(1248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 03:10:37.807: E/AndroidRuntime(1248): at dalvik.system.NativeStart.main(Native Method)
01-09 03:10:37.807: E/AndroidRuntime(1248): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:961)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.Activity.performStart(Activity.java:5018)
01-09 03:10:37.807: E/AndroidRuntime(1248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
01-09 03:10:37.807: E/AndroidRuntime(1248): ... 11 more
答案 0 :(得分:0)
Logcat说&#34;您的内容必须有一个ListView,其id属性为&#39; android.R.id.list&#39;&#34;
根据文件ListFragmant
your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)
是否在flista_professores布局中?
答案 1 :(得分:0)
崩溃是因为你需要将listview的id设为android.R.id.list
像这样更改布局xml flista_professores
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:divider="@color/azul_linha"
android:dividerHeight="1dp"
style="@style/DropDownListView.Cadernotema">
</ListView>