我在尝试使用自定义列表视图将布局扩展为片段活动时遇到了很多麻烦,我在很多页面中搜索了关于该主题的每个问题,但我找不到问题,我会离开我的代码和错误的日志猫。非常感谢
Costum适配器:
public class CustomAdapter extends BaseAdapter implements OnClickListener {
/*********** Declare Used Variables *********/
private PlaceholderFragment activity;
private android.support.v4.app.Fragment fragment;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(PlaceholderFragment customListView, ArrayList d,Resources resLocal) {
/********** Take passed values **********/
activity = customListView;
data=d;
res = resLocal;
/*********** Layout inflator to call external xml layout () ***********/
inflater = ( LayoutInflater )activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public TextView textWide;
public ImageView image;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.tabitem, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( ListModel ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.text.setText( tempValues.getCompanyName() );
holder.text1.setText( tempValues.getUrl() );
holder.image.setImageResource(
res.getIdentifier(
"com.androidexample.customlistview:drawable/"+tempValues.getImage()
,null,null));
/******** Set Item Click Listner for LayoutInflater for each row *******/
vi.setOnClickListener(new OnItemClickListener( position ));
}
return vi;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked=====");
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
PlaceholderFragment sct = (PlaceholderFragment)activity;
/**** Call onItemClick Method inside CustomListViewAndroidExample Class ( See Below )****/
sct.onItemClick(mPosition);
}
}
PlaceholderFragment(片段活动):
public class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
ListView list;
CustomAdapter adapter;
public PlaceholderFragment CustomListView = null;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
CustomListView = this;
setListData();
Resources res =getResources();
ListView list= ( ListView )view.findViewById( R.id.list1 ); // List defined in XML ( See Below )
adapter=new CustomAdapter( CustomListView, CustomListViewValuesArr,res );
list.setAdapter( adapter );
return view;
}
public void setListData()
{
for (int i = 0; i < 11; i++) {
final ListModel sched = new ListModel();
/******* Firstly take data in model object ******/
sched.setCompanyName("Company "+i);
sched.setImage("image"+i);
sched.setUrl("http:\\www."+i+".com");
/******** Take Model Object in ArrayList **********/
CustomListViewValuesArr.add( sched );
}
}
/***************** This function used by adapter ****************/
public void onItemClick(int mPosition)
{
}
public static Activity getApplication() {
// TODO Auto-generated method stub
return null;
}
public LayoutInflater getSystemService(String layoutInflaterService) {
// TODO Auto-generated method stub
return null;
}
public PlaceholderFragment() {
}
Log cat(错误):
01-13 14:43:48.229: E/AndroidRuntime(24059): FATAL EXCEPTION: main
01-13 14:43:48.229: E/AndroidRuntime(24059): java.lang.NullPointerException
01-13 14:43:48.229: E/AndroidRuntime(24059): at com.ipssum.demofood.CustomAdapter.getView(CustomAdapter.java:77)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.ListView.onMeasure(ListView.java:1158)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.View.measure(View.java:15592)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.View.measure(View.java:15592)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.View.measure(View.java:15592)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.View.measure(View.java:15592)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.View.measure(View.java:15592)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.View.measure(View.java:15592)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-13 14:43:48.229: E/AndroidRuntime(24059): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2180)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.View.measure(View.java:15592)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1878)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4356)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.Choreographer.doFrame(Choreographer.java:532)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.os.Handler.handleCallback(Handler.java:725)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.os.Handler.dispatchMessage(Handler.java:92)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.os.Looper.loop(Looper.java:137)
01-13 14:43:48.229: E/AndroidRuntime(24059): at android.app.ActivityThread.main(ActivityThread.java:5099)
01-13 14:43:48.229: E/AndroidRuntime(24059): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 14:43:48.229: E/AndroidRuntime(24059): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 14:43:48.229: E/AndroidRuntime(24059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
01-13 14:43:48.229: E/AndroidRuntime(24059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
01-13 14:43:48.229: E/AndroidRuntime(24059): at dalvik.system.NativeStart.main(Native Method)