我正在研究基于片段的学校项目。我已经制作了XML文件,但我在nearfragment.java中收到错误。你能否告诉我错误的位置,因为我熟悉Activity但不熟悉Fragment。请帮我纠正错误。
near_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="{relativePackage}.${activityClass}" >
<ListView
android:id="@+id/list_li"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
near_layout_mylist.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" >
<ImageView
android:id="@+id/icon_list"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33" />
<TextView
android:id="@+id/textView1_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
CustomListNearAdapter.java
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListNearAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListNearAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.near_layout_mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.near_layout_mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item_list);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_list);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1_list);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description "+itemname[position]);
return rowView;
};
}
NearFragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
/**
* Created by Ratan on 7/29/2015.
*/
public class NearFragment extends Fragment {
ListView list;
String[] itemname ={
"Safari",
"Camera",
"Global",
"FireFox",
"UC Browser",
"Android Folder",
"VLC Player",
"Cold War"
};
Integer[] imgid={
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.near_layout, container,false);
CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);
ListView listView = (ListView) view.findViewById(R.id.list_item);
listView.setAdapter(new CustomListNearAdapter(getActivity()));
return view;
}
}
错误在于nearfragment,我无法为Fragment编写代码。所以请给我一些指示。
答案 0 :(得分:1)
在您的代码中,在设置adapter
时,您没有将正确的值传递给构造函数,意味着在您的代码中的行。
listView.setAdapter(new CustomListNearAdapter(getActivity()));
在上面的行中有问题,所以用下面的代码行替换代码。
CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid);
ListView listView = (ListView) view.findViewById(R.id.list_li);
listView.setAdapter(adapter);
答案 1 :(得分:1)
在下面的行中使用 getActivity()而不是这个
CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);
并将适配器设置为listview,如bellow
listView.setAdapter(adapter);
答案 2 :(得分:1)
我建议您在加载活动后编写ListView
的代码。所以让你的代码如下:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.near_layout, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid);
ListView listView = (ListView) view.findViewById(R.id.list_item);
listView.setAdapter(new CustomListNearAdapter(getActivity()));
}
由于尚未创建活动,getActivity()
可能会导致此处出现问题。此外,您没有正确初始化适配器。您未提供所有值。
答案 3 :(得分:1)
像这样重新调整你的代码。
View view = inflater.inflate(R.layout.near_layout, container,false);
ListView listView = (ListView) view.findViewById(R.id.list_item);
listView.setAdapter(new CustomListNearAdapter(getActivity(), itemname, imgid));
return view;