Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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 刷新ViewPager活动片段中的ListView时崩溃_Android_Listview_Android Listview_Android Fragments_Android Viewpager - Fatal编程技术网

Android 刷新ViewPager活动片段中的ListView时崩溃

Android 刷新ViewPager活动片段中的ListView时崩溃,android,listview,android-listview,android-fragments,android-viewpager,Android,Listview,Android Listview,Android Fragments,Android Viewpager,我有一个活动,里面有两个片段。一个片段用于向ListView添加项目,另一个片段包含ListView。现在我必须重新启动应用程序,以便在添加项目后刷新ListView,但我正在尝试解决这个问题。以下是方法: 我尝试调用方法从ViewPager刷新ListView,如下所示: @Override public void onPageSelected(int position) { library.populateListView(getApplicationContext(), libra

我有一个活动,里面有两个片段。一个片段用于向ListView添加项目,另一个片段包含ListView。现在我必须重新启动应用程序,以便在添加项目后刷新ListView,但我正在尝试解决这个问题。以下是方法:

我尝试调用方法从ViewPager刷新ListView,如下所示:

@Override
public void onPageSelected(int position) {
    library.populateListView(getApplicationContext(), library.lView); //this is line 47
    aBar.setSelectedNavigationItem(position);
}
这是库片段内的populateListView方法:

public void populateListView(Context context, ListView listView){   
    CustomListViewAdapter customAdapter = new CustomListViewAdapter(getDatabaseArrayList(context), getActivity());  
    listView.setAdapter(customAdapter); //this is line 122
    customAdapter.notifyDataSetChanged();
}
以及getDatabaseArrayList方法代码:

private ArrayList<RecordDetails> getDatabaseArrayList(Context context){
    DatabaseHandler dbHandler = new DatabaseHandler(context);
    Cursor c = dbHandler.getAllRecordedFiles();
    ArrayList<RecordDetails> components = new ArrayList<RecordDetails>();
    RecordDetails element;
    if(c.moveToFirst()){
        element = new RecordDetails();
        element.setTitle(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_NAME)));
        element.setDate(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DATE)));
        element.setDuration(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DURATION)));
        components.add(element);

        while (c.moveToNext())
        {
            element = new RecordDetails();
            element.setTitle(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_NAME)));
            element.setDate(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DATE)));
            element.setDuration(c.getString(c.getColumnIndex(SQLiteHelper.MUSIC_DURATION)));
            components.add(element);
        }
    }
    return components;
}
如果我需要发布完整的日志,请告诉我,因为我只发布了我认为最重要的部分

更新---------------

完整onCreateView代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_library, container, false);

    // ListView
    lView = (ListView) v.findViewById(R.id.listView);
    lView.setOnItemClickListener(this);
    registerForContextMenu(lView);

    //Other variables
    components = new ArrayList<RecordDetails>();
    components = getDatabaseArrayList(getActivity());
    db = new DatabaseHandler(getActivity());
    mp = new MediaPlayer();
    playPause = (Button) v.findViewById(R.id.playPause);
    playPause.setOnClickListener(this);
    sBar = (SeekBar) v.findViewById(R.id.seekBar1);
    sBar.setOnSeekBarChangeListener(this);
    seekHandler = new Handler();

    populateListView(getActivity());
    seekHandler.post(run);

    return v;
}
更新2--------

库片段XML代码:

<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"
android:background="@drawable/gradient_background"
tools:context=".LibraryFragment" >

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBar1"
    android:layout_below="@+id/view1"
    android:layout_centerHorizontal="true"
    android:focusable="false"
    android:focusableInTouchMode="false" >

</ListView>

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/gradient_shadow_transparent" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="53dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp" />

<Button
    android:id="@+id/playPause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Play" />

<TextView
    android:id="@+id/current"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/seekBar1"
    android:layout_alignParentLeft="true"
    android:text="0:03"
    android:layout_marginLeft="5dp" />

<TextView
    android:id="@+id/duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/seekBar1"
    android:layout_alignParentRight="true"
    android:text="0:17"
    android:layout_marginRight="5dp" />

我认为您不需要从ViewPageActivity发送listview。将listview实例保存在库片段本身中,并将其存储在onCreateView中

将您的populateListView更改为

public void populateListView(Context context){   
    CustomListViewAdapter customAdapter = new CustomListViewAdapter(getDatabaseArrayList(context), getActivity());  
    lView.setAdapter(customAdapter); //this is line 122
    customAdapter.notifyDataSetChanged();
}

如果这段代码是片段,那么您需要从

@Override
public void onPageSelected(int position) {
 library.populateListView(getApplicationContext(), library.lView); //this is line 47
 aBar.setSelectedNavigationItem(position);
}

您还必须在此处进行更改,因为在populateListView中有两个参数,而您仅使用上下文执行。所以改变它

  populateListView(getActivity());

我想问题可能在这里

   components = getDatabaseArrayList(getActivity());
   db = new DatabaseHandler(getActivity());
getDatabaseArrayList是您正在执行的函数,在此之后,您已经初始化了数据库类。所以它会在这里坠毁。所以改变

   db = new DatabaseHandler(getActivity());
   components = getDatabaseArrayList(getActivity());
你试过这个吗

  public void populateListView(Context context){   
   CustomListViewAdapter customAdapter = new CustomListViewAdapter(components, getActivity());  
   lView.setAdapter(customAdapter); //this is line 122
   customAdapter.notifyDataSetChanged();
  }

我从你的代码中了解到,你试图用新数据刷新listView,这会导致你的应用程序崩溃

要安全地刷新listview,需要在customAdapter中创建一个名为setNewDataArrayList newData的方法。用正在使用的数据类型替换对象,并在此方法中执行以下操作:

public void setNewDAta(ArrayList<object> newData){
    this.data = newData;
}
public void setListViewData(ArrayList<object> newData){
    customAdapter.setNewDAta(newData);
    customAdapter.notifyDataSetChanged();
}

我已经试过了,但是它崩溃了,说lView为null。你能发布onCreateView for library片段吗?是否在此方法内将listview分配给lView?是的,我正在onCreateView方法内将listview分配给lView。我马上用代码更新我的问题。只要检查你的片段,如果你在我检查的任何地方重置lView变量,我不会在任何地方重置它。我在onCreateView中实例化它,然后再也不更改或重置它。请发布日志猫,直到library.lView引起的行为空@AbdEl RahmanEl Tamawy我用完整的logcat更新了我的答案,但我担心这根本不是由行引起的。@GauravGupta是的,但我如何解决它呢?是ListView v.findViewByIdR.id.ListView;是否也返回null?另外,发布xml片段库否,此代码在保存片段的活动中。此populateListView函数是否在您的片段类中?是的,populateListView位于片段中。请再次检查我的答案。我知道,我无意中发布了不带参数的代码,但实际上我正在添加ListView,但它仍然崩溃。
  public void populateListView(Context context){   
   CustomListViewAdapter customAdapter = new CustomListViewAdapter(components, getActivity());  
   lView.setAdapter(customAdapter); //this is line 122
   customAdapter.notifyDataSetChanged();
  }
public void setNewDAta(ArrayList<object> newData){
    this.data = newData;
}
public void setListViewData(ArrayList<object> newData){
    customAdapter.setNewDAta(newData);
    customAdapter.notifyDataSetChanged();
}