Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义适配器listview无法正常工作_Java_Android_Listview_Baseadapter_Custom Adapter - Fatal编程技术网

Java 自定义适配器listview无法正常工作

Java 自定义适配器listview无法正常工作,java,android,listview,baseadapter,custom-adapter,Java,Android,Listview,Baseadapter,Custom Adapter,自定义适配器有问题。我无法显示listview。当我运行应用程序时,它停止运行。 请看下面的代码 先谢谢你 AllProductsActivity.java XML android:layout\u width=“fill\u parent” >android:layout\u height=“fill\u parent” >android:orientation=“垂直” >android:weightSum=“1” >android:baselineAligned=“false”> >

自定义适配器有问题。我无法显示listview。当我运行应用程序时,它停止运行。 请看下面的代码

先谢谢你

AllProductsActivity.java XML

android:layout\u width=“fill\u parent” >android:layout\u height=“fill\u parent” >android:orientation=“垂直” >android:weightSum=“1” >android:baselineAligned=“false”> > > >android:layout\u width=“匹配父项” >android:layout\u height=“包装内容” >android:layout_weight=“0.1”> > > >android:id=“@+id/textView” >android:layout\u width=“匹配父项” >android:layout_height=“30sp” > >android:text=“产品名称价格Qnt” >android:layout_weight=“0.1”/> > >android:id=“@+id/list” >android:layout\u width=“fill\u parent” >android:layout_height=“347sp” >android:layout_weight=“0.60”/> > >android:id=“@+id/sendOrder” >android:layout\u width=“匹配父项” >android:layout_height=“50sp” >android:background=“#ffff8800” >android:fontfamine=“无衬线小帽” >android:text=“发送订单” >android:textColorLink=“@color/colorAccent” >android:textSize=“20sp” >android:layout_weight=“0.2”/> > > > 请参阅此

public class yourAdapter extends ArrayAdapter<Actors> {

LayoutInflater vi;
ViewHolder holder;
int resource;
private final Activity context;
private final ArrayList<Actors> details;

public WifiAdapter(Activity context, int resource , ArrayList<Actors> details) {
super(context, resource, details);
vi =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.resource = resource;
this.details = details;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
    holder = new ViewHolder();
    v = vi.inflate(resource, null);
    holder.Type = (TextView) v.findViewById(R.id.type);
    .......//your remaining code
    v.setTag(holder);
}
else {
    holder =(ViewHolder) v.getTag();
}


holder.Name.setText(details.get(position).getType());
..........//ur remaining code 


return v;
}

static class ViewHolder {
public TextView Type;
//your remaing code
}
}
公共类yourAdapter扩展了ArrayAdapter{
拉平机vi;
视窗座;
智力资源;
私人最终活动背景;
私人最终ArrayList详细信息;
公共WifiAdapter(活动上下文、int资源、ArrayList详细信息){
超级(上下文、资源、细节);
vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
this.context=上下文;
这个资源=资源;
this.details=详细信息;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
holder=新的ViewHolder();
v=vi.充气(资源,空);
holder.Type=(TextView)v.findViewById(R.id.Type);
……您剩余的代码
v、 setTag(支架);
}
否则{
holder=(ViewHolder)v.getTag();
}
holder.Name.setText(details.get(position.getType());
……剩余代码
返回v;
}
静态类视窗夹{
公共文本视图类型;
//你的重播代码
}
}

发生此问题是因为您的
列表视图
中的ID错误,请将您的
列表视图
小部件更改为:

 <ListView
         android:id="@android:id/list"
         android:layout_width="fill_parent"
         android:layout_height="347sp"
         android:layout_weight="0.60" />


将stacktrace放在此处我正在android设备上运行应用程序,因为android studio存在相同的问题,无法查看stacktrace。您可以使用android监视器工具查看stacktrace,使用错误过滤并查看异常。我刚刚添加了stacktrace。问题在您的XML文件中。把它放在你的问题上
FATAL EXCEPTION: main
                                                                            Process: com.example.stratos.fastsupply, PID: 11074
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stratos.fastsupply/com.example.stratos.fastsupply.AllProductsActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
                                                                                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                at android.os.Looper.loop(Looper.java:193)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                                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:824)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                                at dalvik.system.NativeStart.main(Native Method)
                                                                             Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                                                at android.app.ListActivity.onContentChanged(ListActivity.java:243)
                                                                                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:344)
                                                                                at android.app.Activity.setContentView(Activity.java:1948)
                                                                                at com.example.stratos.fastsupply.AllProductsActivity.onCreate(AllProductsActivity.java:57)
                                                                                at android.app.Activity.performCreate(Activity.java:5343)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                                at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                at android.os.Looper.loop(Looper.java:193) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                                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:824) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                                at dalvik.system.NativeStart.main(Native Method) 
>     <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
>     android:layout_width="fill_parent"
>     android:layout_height="fill_parent"
>     android:orientation="vertical"
>     android:weightSum="1"
>     android:baselineAligned="false">
>     <!-- Main ListView
>          Always give id value as list(@android:id/list)
>     -->
> 
>     <SearchView
>         android:layout_width="match_parent"
>         android:layout_height="wrap_content"
>         android:layout_weight="0.1">
>     </SearchView>
> 
>     <TextView
>         android:id="@+id/textView"
>         android:layout_width="match_parent"
>         android:layout_height="30sp"
> 
>         android:text="Product Name     Price      Qnt"
>         android:layout_weight="0.1"/>
> 
>     <ListView
>         android:id="@+id/list"
>         android:layout_width="fill_parent"
>         android:layout_height="347sp"
>         android:layout_weight="0.60" />
> 
>     <Button
>         android:id="@+id/sendOrder"
>         android:layout_width="match_parent"
>         android:layout_height="50sp"
>         android:background="#ffff8800"
>         android:fontFamily="sans-serif-smallcaps"
>         android:text="Send Order"
>         android:textColorLink="@color/colorAccent"
>         android:textSize="20sp"
>         android:layout_weight="0.2"/>
> 
> 
> </LinearLayout>
public class yourAdapter extends ArrayAdapter<Actors> {

LayoutInflater vi;
ViewHolder holder;
int resource;
private final Activity context;
private final ArrayList<Actors> details;

public WifiAdapter(Activity context, int resource , ArrayList<Actors> details) {
super(context, resource, details);
vi =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.resource = resource;
this.details = details;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
    holder = new ViewHolder();
    v = vi.inflate(resource, null);
    holder.Type = (TextView) v.findViewById(R.id.type);
    .......//your remaining code
    v.setTag(holder);
}
else {
    holder =(ViewHolder) v.getTag();
}


holder.Name.setText(details.get(position).getType());
..........//ur remaining code 


return v;
}

static class ViewHolder {
public TextView Type;
//your remaing code
}
}
 <ListView
         android:id="@android:id/list"
         android:layout_width="fill_parent"
         android:layout_height="347sp"
         android:layout_weight="0.60" />