我有一个片段类,它是viewpager中其他几个类的一部分。我想创建一个ListView
通过和数组适配器,并在其中一个片段上显示listview中的内容。这里的问题是当我实现MyLogs.add(new actlog("String","String");
程序崩溃时,但当我添加删除这行代码时,应用程序运行但片段为空。这通常是我在活动类上设置和显示ListView
的方式,但它对片段类的工作方式不同。
片段类
package com.example.swipeview;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentOne extends Fragment {
Context context;
public List<actlog> MyLogs = new ArrayList<actlog>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view= inflater.inflate(R.layout.fragment_one_layout,container, false);
MyLogs.add(new actlog("12:00 AM",""));
MyLogs.add(new actlog("12:30 AM",""));
MyLogs.add(new actlog("1:00 AM",""));
MyLogs.add(new actlog("1:30 AM",""));
MyLogs.add(new actlog("2:00 AM",""));
MyLogs.add(new actlog("2:30 AM",""));
MyLogs.add(new actlog("3:00 AM",""));
ArrayAdapter<actlog> adapter = new MyListAdapter();
ListView list = (ListView) view.findViewById(R.id.fragmentonelistview);
list.setAdapter(adapter);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public class MyListAdapter extends ArrayAdapter<actlog>{
public MyListAdapter(){
super(getActivity(),R.layout.item_view, MyLogs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_view, parent, false);
actlog currentlog = MyLogs.get(position);
TextView textview = (TextView) convertView.findViewById(R.id.timeView);
textview.setText(currentlog.getTime());
return convertView;
}
}//end of listadapter class
}//end of class
actlog class
package com.example.swipeview;
import java.io.Serializable;
public class actlog implements Serializable{
public String time;
public String log;
public actlog(String time, String log) {
super();
this.time = time;
this.setLog(log);
}
public String getTime(){
return time;
}
public String getLog(){
return log;
}
public void setLog(String log){
this.log = log;
}
}
logcat的
09-03 19:50:39.989: W/dalvikvm(1548): threadid=1: thread exiting with uncaught exception (group=0xb1aa4ba8)
09-03 19:50:40.019: E/AndroidRuntime(1548): FATAL EXCEPTION: main
09-03 19:50:40.019: E/AndroidRuntime(1548): Process: com.example.swipeview, PID: 1548
09-03 19:50:40.019: E/AndroidRuntime(1548): java.lang.NullPointerException
09-03 19:50:40.019: E/AndroidRuntime(1548): at com.example.swipeview.FragmentOne$MyListAdapter.getView (FragmentOne.java:63)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.widget.ListView.onMeasure(ListView.java:1175)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.view.View.measure(View.java:16497)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.view.View.measure(View.java:16497)
09-03 19:50:40.019: E/AndroidRuntime(1548): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-03 19:50:40.019: E/AndroidRuntime
答案 0 :(得分:0)
在你的适配器的getView()中你喜欢这个
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
OR
LayoutInflater inflater = LayoutInflater.from(getActivity());
或在适配器的构造函数中初始化Context变量,如
public MyListAdapter(){
super(getActivity(),R.layout.item_view, MyLogs);
Context context = getactivity();
}
我认为这可以解决您的问题。