我正在使用Fragments
编写Android应用程序。
所以我创建了一个ListView
适配器和一个新的Fragment
,但是当我尝试在Fragments
之间移动时,我的应用程序会崩溃。
我的适配器:
public class todoAdapter extends ArrayAdapter {
Context mContext;
int layoutResourceId;
Item data[]=null;
public todoAdapter (Context mContext,int layoutResourceId,Item data[]) {
super(mContext,layoutResourceId,data);
this.mContext=mContext;
this.layoutResourceId=layoutResourceId;
this.data=data;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
if(convertView==null) {
LayoutInflater inflater=((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId,parent,false);
}
Item item = data[position];
TextView Label=(TextView) convertView.findViewById(R.id.textViewLabel);
ImageView img=(ImageView) convertView.findViewById(R.id.textViewImage);
TextView Desc=(TextView) convertView.findViewById(R.id.textViewDesc);
Label.setText(item.itemname);
Label.setTag(item.Itemid);
Desc.setText(item.Describtion);
//img.setImageDrawable(downloadImage(item.image));
img.setImageResource(R.mipmap.ic_launcher);
return convertView;
}}
和我的Fragment
public class todoFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.todo, container, false);
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Item[] data=new Item[2];
// public Item(int Itemid,String itemname,String image,String Describtion) {
data[0]=new Item(1,"HELLO","blabla","blablabla");
data[1]=new Item(1,"HELLO2","blabla","blablabla");
ListView x=(ListView) rootView.findViewById(R.id.todolistview);
todoAdapter adapter=new todoAdapter(getActivity(),R.layout.todo_item,data);
x.setAdapter(adapter);
x.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(getActivity(),"FFF",Toast.LENGTH_LONG).show();
}
});
//Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG);
return rootView;
}
}
logcat的:
08-11 22:35:58.186 3752-3752/syriatel.nowwhat D/AndroidRuntime﹕ Shutting down VM
08-11 22:35:58.186 3752-3752/syriatel.nowwhat W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x64d69b20)
08-11 22:35:58.186 3752-3752/syriatel.nowwhat I/Process﹕ Sending signal. PID: 3752 SIG: 9
08-11 22:35:58.186 3752-3752/syriatel.nowwhat D/AndroidRuntime﹕ procName from cmdline: syriatel.nowwhat
08-11 22:35:58.186 3752-3752/? E/AndroidRuntime﹕ in writeCrashedAppName, pkgName :syriatel.nowwhat
08-11 22:35:58.186 3752-3752/? D/AndroidRuntime﹕ file written successfully with content: syriatel.nowwhat StringBuffer : ;syriatel.nowwhat
08-11 22:35:58.186 3752-3752/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: syriatel.nowwhat, PID: 3752
java.lang.NullPointerException
at syriatel.nowwhat.ui.activity.MainActivity$1.run(MainActivity.java:101)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
对不起,看起来问题不在于Fragment本身
它是一个处理程序编辑元素,当移动到另一个片段时导致问题