【发布时间】:2014-12-05 04:44:09
【问题描述】:
这是我遇到的错误
12-05 13:26:18.102: E/AndroidRuntime(10101): FATAL EXCEPTION: main
12-05 13:26:18.102: E/AndroidRuntime(10101): Process: com.example.project_1_4, PID: 10101
12-05 13:26:18.102: E/AndroidRuntime(10101): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.project_1_4/
com.example.project_1_4.MainActivity}: android.view.InflateException: Binary XML file line #14:
Error inflating class fragment
我的 xml 文件 - 我想我在发帖时错过了括号。现在我纠正它。
<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="@android:id/list"
android:layout_width="match_parent"
android:layout_height="410dp" >
</ListView>
</LinearLayout>
源代码
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.zip.Inflater;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Dday_fragment extends ListFragment{
ArrayList<Detail> detailList = new ArrayList<Detail>();
ArrayList<String> detailName = new ArrayList<String>();
DetailOperation DetailDBOperation;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = new View(getActivity());
try{
view = inflater.inflate(R.layout.dday_fragment, container, false);
}catch (InflateException e){
}
DetailDBOperation = new DetailOperation(null);
try {
DetailDBOperation.open();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(DetailDBOperation.getAllDetail()!=null){
detailList = DetailDBOperation.getAllDetail();
}else{
detailList = null;
}
int size = detailList.size();
for(int i = 0; i < size; i++){
detailName.add(detailList.get(i).getSubjectName());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, detailName);
setListAdapter(adapter);
return view;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Detail_fragment dtf = (Detail_fragment) getFragmentManager().findFragmentById(R.layout.detail_fragment);
dtf.change(position);
getListView().setSelector(android.R.color.holo_red_dark);
}
}
这是 Dday_fragment.java 的完整代码 如果我不使用 ListFragment,如何使用 onListItemClick? 我想要的是在 dday_fragment 中显示项目列表, 还有另一个xml随着用户选择的项目ID而变化。 另一个 xml 将显示项目的详细信息。 所以我使用 onListItemClick 在另一个 xml 上更改和设置文本。
【问题讨论】:
-
能否请您发布 dday_fragment 的完整 xml 代码?
-
如果您使用
LisstFragment扩展片段,则无需在xml 文件中使用ListView。 -
就是完整的xml代码哈哈
标签: android fragment android-inflate