我的Android应用程序给出了未附加到活动的片段错误。

我的Android应用程序给出了未附加到活动的片段错误。,android,Android,//这是我的代码 我的目标是在片段上添加一个自定义listview,并在创建活动时启动它 //FragmentSongs.java import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget

//这是我的代码
我的目标是在片段上添加一个自定义listview,并在创建活动时启动它

//FragmentSongs.java

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class FragmentSongs extends ListFragment implements OnItemClickListener {
    
    ListView listview;
     String[] Songs=getResources().getStringArray(R.string.songs);
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        
            MyAdapter adapter=new MyAdapter(getActivity(),Songs);
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(this);
            View myview=inflater.inflate(R.layout.fragment_songs, container,false);
            return myview;
           }
    
          
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Toast.makeText(getActivity(), Songs[position], Toast.LENGTH_SHORT).show();
        
    }   
    
}
//activitymain.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/activitylayout"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragment1"
        android:name="android.app.ListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

//

//
这是我要附加到activitymain的片段的名称

fragment_songs.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:baselineAligned="false" >

    <ListView
        android1:id="@+id/listView1"
        android1:layout_width="match_parent"
        android1:layout_height="0dp"
        android1:layout_weight="1" >
    </ListView>

</LinearLayout>

//songlist_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="#1A237E" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Large Text"
        android:background="#C5CAE9"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:background="#C5CAE9"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/textView1"
        android:layout_toRightOf="@+id/textView2"
        android:background="#C5CAE9"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

插入活动片段有两种方法,您可以同时使用这两种方法

第一种方法是: 在活动的布局文件中声明片段。 但在这种情况下,片段将像视图一样固定在活动上

第二种形式是: 以编程方式将片段添加到现有视图组。 在这种情况下,您可以随时将该片段添加到活动中,用另一个片段替换它,并在需要时删除该片段

你可以看到我告诉你的,看这个Android开发者的链接,看“给活动添加一个片段”

注意:如果您决定以编程方式添加片段,则不必在XML活动中引用它。另一方面,这些线路将:

    FragmentSongs songs=new FragmentSongs();
    FragmentManager fm=getSupportFragmentManager();
    FragmentTransaction transaction=fm.beginTransaction();
    transaction.add(R.id.content,songs);
    transaction.commit();
R.id.content:它是在activitymain.xml LinearLayout上以编程方式添加片段的容器的id。正如你看到的,你不喜欢这个,但是你链接了片段的id,这是一个错误

别忘了,你不应该使用两种方法来添加片段,所以选择一种,希望提示你的方法有用。

检查此->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="#1A237E" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Large Text"
        android:background="#C5CAE9"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:background="#C5CAE9"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/textView1"
        android:layout_toRightOf="@+id/textView2"
        android:background="#C5CAE9"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>
    FragmentSongs songs=new FragmentSongs();
    FragmentManager fm=getSupportFragmentManager();
    FragmentTransaction transaction=fm.beginTransaction();
    transaction.add(R.id.content,songs);
    transaction.commit();