Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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
Android 碎片和适配器故障_Android_Android Fragments_Adapter - Fatal编程技术网

Android 碎片和适配器故障

Android 碎片和适配器故障,android,android-fragments,adapter,Android,Android Fragments,Adapter,我正在开发一个小应用程序,它应该显示3个屏幕:对话、论文和选择。几天前我发现了一些碎片,我想在我的应用程序中使用这个功能,但不幸的是,由于特定的原因,它不起作用 我的片段: package info.androidhive.tabsswipe; import java.io.IOException; import java.util.List; import be.unamur.confpers.beans.Paper; import be.unamur.confpers.xml.XMLPars

我正在开发一个小应用程序,它应该显示3个屏幕:对话、论文和选择。几天前我发现了一些碎片,我想在我的应用程序中使用这个功能,但不幸的是,由于特定的原因,它不起作用

我的片段:

package info.androidhive.tabsswipe;
import java.io.IOException;
import java.util.List;
import be.unamur.confpers.beans.Paper;
import be.unamur.confpers.xml.XMLParser2;
import info.androidhive.tabsswipe.R;
import info.androidhive.tabsswipe.adapter.PaperAdapter;
import android.app.ListFragment;
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.ListView;
import be.unamur.confpers.beans.Paper;
import be.unamur.confpers.xml.XMLParser2;
import be.unamur.confpers.*;

public class PapersFragment extends Fragment {

    private PaperAdapter listAdapter;
    private Context myContext;
    List<Paper> papers = null;

    private List<Paper> papersParser () {

        try {
            XMLParser2 parser = new XMLParser2();
            papers = parser.parse(getActivity().getApplicationContext().getAssets().open("talks.xml"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return papers;
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_papers, container, false);

        ListView lv = (ListView) getActivity().findViewById(R.id.check1);
        listAdapter = new PaperAdapter(getActivity(),papers);
        //lv.setAdapter(listAdapter);

        return v;
    }
}
注释后,它会加载,但一旦我取消注释,它就会崩溃,并出现以下错误:

12-06 11:28:05.240:E/AndroidRuntime(1362):java.lang.ClassCastException:android.widget.TextView无法强制转换为android.widget.ListView


参照这条精确的线。

您将复选框转换为ListView

应该是这样的。
ListView lv=(ListView)getActivity().findViewById(R.id.list)

谢谢你的收入,我正在尝试!它仍然会崩溃,即使在清理、构建之后也是如此。:/你还有其他想法吗?试试这一行ListView lv=(ListView)v.findviewbyd(R.id.ListviewName);尝试打印并检查papers对象是否正常工作,然后再将其传递给Adapter谢谢任何人的帮助,我忘记填充列表了!即使在清洁、建造之后,它仍然会崩溃。你还有其他想法吗?是的,不幸的是…现在它说“12-06 13:08:08.736:E/AndroidRuntime(2082):在info.androidhive.tabsswipe.PapersFragment.onCreateView(PapersFragment.java:49)”请像这样编辑=>
v.findViewById(R.id.list)用于listView分配。感谢@Nagaraja,我也尝试过了,但仍然不起作用……感谢任何人的帮助,我忘记填充列表了!感谢任何人的帮助,我忘记填写列表了!
package info.androidhive.tabsswipe.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import be.unamur.confpers.beans.Paper;
import info.androidhive.tabsswipe.R;

import java.util.ArrayList;
import java.util.List;

public class PaperAdapter extends BaseAdapter implements OnClickListener {

    private Context context;
    private List<Paper> papers;
    private LayoutInflater inflater;

    public PaperAdapter(Context context, List<Paper> papers) {
        this.context = context;
        this.papers = papers;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount(){
        return papers.size();
    }

    @Override
    public Object getItem(int position) {
        return papers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        // We only create the view if its needed
        if (view == null) {
            view = inflater.inflate(R.layout.child_row, null);

            // Set the click listener for the checkbox
            view.findViewById(R.id.check1).setOnClickListener(this);
        }

        Paper p = (Paper) getItem(position);

        // Set the example text and the state of the checkbox
        CheckBox cb = (CheckBox) view.findViewById(R.id.check1);
        //cb.setChecked(p.isSelected());
        // We tag the data object to retrieve it on the click listener.

        TextView paper = (TextView)view.findViewById(R.id.papername);
        if (paper != null)
            paper.setText(p.getTitle());

        TextView author = (TextView)view.findViewById(R.id.authorname);
        if( author!= null )
            author.setText( p.getAuthor() );

        return view;
    }

    /*@Override
    /** Will be called when a checkbox has been clicked. */
    public void onClick(View view) {

    }


}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" >

   <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_above="@+id/buttonsave" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <TextView
        android:id="@+id/papername"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

<Button
    android:id="@+id/buttonsave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/btn_save" />

<Button
    android:id="@+id/buttonload"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/btn_load" />
</RelativeLayout>
<?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="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >

<TextView
    android:id="@+id/papername"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="50dp"
    android:textSize="18sp"
    android:textStyle="bold" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal" 
android:shrinkColumns="0">

<!-- 2 columns -->

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:paddingLeft="20dp"
     >

    <CheckBox
        android:id="@+id/check1"
        android:layout_height="wrap_content"
        android:layout_width="30dp"
        android:focusable="false"
         />

    <TextView
        android:id="@+id/papername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:textSize="16sp"
        android:textStyle="normal"
        android:layout_marginLeft="5dp" />
</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="50dp"
    android:paddingBottom="15dp" >

    <TextView
        android:id="@+id/authorname"
        android:layout_span="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:textSize="16sp"
        android:textStyle="italic"
        android:layout_marginRight="5dp" />
</TableRow>

</TableLayout>
//lv.setAdapter(listAdapter);
I gusse here is the error in  your code:

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.fragment_papers, container, false);

            ListView lv = (ListView) getActivity().findViewById(R.id.check1);
//Here your casting checkBox to Listview Pleas Check it out

            listAdapter = new PaperAdapter(getActivity(),papers);
            //lv.setAdapter(listAdapter);

            return v;
        }

just replace it like

 ListView lv = (ListView) v.findViewById(R.id.ListviewName);