Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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/195.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 Android中的片段活动出错_Java_Android_Fragment_Orientation - Fatal编程技术网

Java Android中的片段活动出错

Java Android中的片段活动出错,java,android,fragment,orientation,Java,Android,Fragment,Orientation,我正在根据教程尝试一个片段应用程序,出现以下错误: 代码如下: Main Activity.java package com.example.fragmentapp; import android.os.Bundle; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity implements ListFragment.Com

我正在根据教程尝试一个片段应用程序,出现以下错误:

代码如下: Main Activity.java

package com.example.fragmentapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity implements
        ListFragment.Communicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void Message(String OS_Name) {
        DetailFragment detailfragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detail_Fragment);
        if (detailfragment != null && detailfragment.isInLayout()) {
            detailfragment.setText(OS_Name);
        }
        else {
            Intent intent = new Intent(getApplicationContext(),
                    DetailActivity.class);
            Bundle extras = new Bundle();
            extras.putString(DetailActivity.os_name, OS_Name);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class DetailActivity extends Activity {

    public static String os_name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            finish();
            return;
        }
        setContentView(R.layout.detail_activity);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String name = extras.getString(os_name);
            DetailFragment detailFragment = (DetailFragment) getFragmentManager()
                    .findFragmentById(R.id.detailFragment);
            detailFragment.setText(name);
        }
    }
}
package com.example.fragmentapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment, container, false);
        return view;
    }

    // we call this method when button from listfragment is clicked
    public void setText(String item) {
        TextView view = (TextView) getView().findViewById(R.id.display_tv);
        view.setText(item);
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ListFragment extends Fragment implements OnClickListener {

    private Communicator communicator;

    Button android_btn, ios_btn, windows_btn;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof Communicator) {
            communicator = (Communicator) activity;
        }
        else {
            throw new
                    ClassCastException(activity.toString()
                    + " must implemenet MyListFragment.Communicator");
        }
    }

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

        // Initialize Views   android_btn = (Button)
        view.findViewById(R.id.android_btn_id);
        ios_btn = (Button)
                view.findViewById(R.id.ios_btn_id);
        windows_btn = (Button)
                view.findViewById(R.id.windows_btn_id);

        // set on click Listeners for buttons  
        android_btn.setOnClickListener(this);
        ios_btn.setOnClickListener(this);
        windows_btn.setOnClickListener(this);

        return view;
    }

    //Create Interface   
    public interface Communicator {
        public void Message(String OS_Name);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.android_btn_id:
                updateFragment("Android");
                break;
            case R.id.ios_btn_id:
                updateFragment("IOS");
                break;

            case R.id.windows_btn_id:
                updateFragment("Windows");
                break;
        }
    }

    private void updateFragment(String OS_Name) {
        communicator.Message(OS_Name);
    }
}
DetailActivity.java的代码

package com.example.fragmentapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity implements
        ListFragment.Communicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void Message(String OS_Name) {
        DetailFragment detailfragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detail_Fragment);
        if (detailfragment != null && detailfragment.isInLayout()) {
            detailfragment.setText(OS_Name);
        }
        else {
            Intent intent = new Intent(getApplicationContext(),
                    DetailActivity.class);
            Bundle extras = new Bundle();
            extras.putString(DetailActivity.os_name, OS_Name);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class DetailActivity extends Activity {

    public static String os_name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            finish();
            return;
        }
        setContentView(R.layout.detail_activity);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String name = extras.getString(os_name);
            DetailFragment detailFragment = (DetailFragment) getFragmentManager()
                    .findFragmentById(R.id.detailFragment);
            detailFragment.setText(name);
        }
    }
}
package com.example.fragmentapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment, container, false);
        return view;
    }

    // we call this method when button from listfragment is clicked
    public void setText(String item) {
        TextView view = (TextView) getView().findViewById(R.id.display_tv);
        view.setText(item);
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ListFragment extends Fragment implements OnClickListener {

    private Communicator communicator;

    Button android_btn, ios_btn, windows_btn;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof Communicator) {
            communicator = (Communicator) activity;
        }
        else {
            throw new
                    ClassCastException(activity.toString()
                    + " must implemenet MyListFragment.Communicator");
        }
    }

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

        // Initialize Views   android_btn = (Button)
        view.findViewById(R.id.android_btn_id);
        ios_btn = (Button)
                view.findViewById(R.id.ios_btn_id);
        windows_btn = (Button)
                view.findViewById(R.id.windows_btn_id);

        // set on click Listeners for buttons  
        android_btn.setOnClickListener(this);
        ios_btn.setOnClickListener(this);
        windows_btn.setOnClickListener(this);

        return view;
    }

    //Create Interface   
    public interface Communicator {
        public void Message(String OS_Name);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.android_btn_id:
                updateFragment("Android");
                break;
            case R.id.ios_btn_id:
                updateFragment("IOS");
                break;

            case R.id.windows_btn_id:
                updateFragment("Windows");
                break;
        }
    }

    private void updateFragment(String OS_Name) {
        communicator.Message(OS_Name);
    }
}
DetailFragment.java的代码

package com.example.fragmentapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity implements
        ListFragment.Communicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void Message(String OS_Name) {
        DetailFragment detailfragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detail_Fragment);
        if (detailfragment != null && detailfragment.isInLayout()) {
            detailfragment.setText(OS_Name);
        }
        else {
            Intent intent = new Intent(getApplicationContext(),
                    DetailActivity.class);
            Bundle extras = new Bundle();
            extras.putString(DetailActivity.os_name, OS_Name);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class DetailActivity extends Activity {

    public static String os_name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            finish();
            return;
        }
        setContentView(R.layout.detail_activity);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String name = extras.getString(os_name);
            DetailFragment detailFragment = (DetailFragment) getFragmentManager()
                    .findFragmentById(R.id.detailFragment);
            detailFragment.setText(name);
        }
    }
}
package com.example.fragmentapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment, container, false);
        return view;
    }

    // we call this method when button from listfragment is clicked
    public void setText(String item) {
        TextView view = (TextView) getView().findViewById(R.id.display_tv);
        view.setText(item);
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ListFragment extends Fragment implements OnClickListener {

    private Communicator communicator;

    Button android_btn, ios_btn, windows_btn;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof Communicator) {
            communicator = (Communicator) activity;
        }
        else {
            throw new
                    ClassCastException(activity.toString()
                    + " must implemenet MyListFragment.Communicator");
        }
    }

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

        // Initialize Views   android_btn = (Button)
        view.findViewById(R.id.android_btn_id);
        ios_btn = (Button)
                view.findViewById(R.id.ios_btn_id);
        windows_btn = (Button)
                view.findViewById(R.id.windows_btn_id);

        // set on click Listeners for buttons  
        android_btn.setOnClickListener(this);
        ios_btn.setOnClickListener(this);
        windows_btn.setOnClickListener(this);

        return view;
    }

    //Create Interface   
    public interface Communicator {
        public void Message(String OS_Name);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.android_btn_id:
                updateFragment("Android");
                break;
            case R.id.ios_btn_id:
                updateFragment("IOS");
                break;

            case R.id.windows_btn_id:
                updateFragment("Windows");
                break;
        }
    }

    private void updateFragment(String OS_Name) {
        communicator.Message(OS_Name);
    }
}
ListFragment.java的代码

package com.example.fragmentapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity implements
        ListFragment.Communicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void Message(String OS_Name) {
        DetailFragment detailfragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detail_Fragment);
        if (detailfragment != null && detailfragment.isInLayout()) {
            detailfragment.setText(OS_Name);
        }
        else {
            Intent intent = new Intent(getApplicationContext(),
                    DetailActivity.class);
            Bundle extras = new Bundle();
            extras.putString(DetailActivity.os_name, OS_Name);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class DetailActivity extends Activity {

    public static String os_name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            finish();
            return;
        }
        setContentView(R.layout.detail_activity);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String name = extras.getString(os_name);
            DetailFragment detailFragment = (DetailFragment) getFragmentManager()
                    .findFragmentById(R.id.detailFragment);
            detailFragment.setText(name);
        }
    }
}
package com.example.fragmentapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment, container, false);
        return view;
    }

    // we call this method when button from listfragment is clicked
    public void setText(String item) {
        TextView view = (TextView) getView().findViewById(R.id.display_tv);
        view.setText(item);
    }
}
package com.example.fragmentapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ListFragment extends Fragment implements OnClickListener {

    private Communicator communicator;

    Button android_btn, ios_btn, windows_btn;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof Communicator) {
            communicator = (Communicator) activity;
        }
        else {
            throw new
                    ClassCastException(activity.toString()
                    + " must implemenet MyListFragment.Communicator");
        }
    }

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

        // Initialize Views   android_btn = (Button)
        view.findViewById(R.id.android_btn_id);
        ios_btn = (Button)
                view.findViewById(R.id.ios_btn_id);
        windows_btn = (Button)
                view.findViewById(R.id.windows_btn_id);

        // set on click Listeners for buttons  
        android_btn.setOnClickListener(this);
        ios_btn.setOnClickListener(this);
        windows_btn.setOnClickListener(this);

        return view;
    }

    //Create Interface   
    public interface Communicator {
        public void Message(String OS_Name);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.android_btn_id:
                updateFragment("Android");
                break;
            case R.id.ios_btn_id:
                updateFragment("IOS");
                break;

            case R.id.windows_btn_id:
                updateFragment("Windows");
                break;
        }
    }

    private void updateFragment(String OS_Name) {
        communicator.Message(OS_Name);
    }
}
布局文件夹中的 activity_main.xml

<?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="horizontal" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detail_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="com.pavan.fragmentdemo.DetailFragment" >
    </fragment>

</LinearLayout>
<?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:background="#FFFF99"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/display_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="40sp" />

</LinearLayout>
<?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:background="#CCFF99"
    android:orientation="vertical"
    android:padding="5dp" >

    <Button
        android:id="@+id/android_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android" />

    <Button
        android:id="@+id/ios_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS" />

    <Button
        android:id="@+id/windows_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows" />

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.pavan.fragmentdemo.DetailFragment" />

</LinearLayout>

详细代码\u fragment.xml

<?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="horizontal" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detail_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="com.pavan.fragmentdemo.DetailFragment" >
    </fragment>

</LinearLayout>
<?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:background="#FFFF99"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/display_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="40sp" />

</LinearLayout>
<?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:background="#CCFF99"
    android:orientation="vertical"
    android:padding="5dp" >

    <Button
        android:id="@+id/android_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android" />

    <Button
        android:id="@+id/ios_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS" />

    <Button
        android:id="@+id/windows_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows" />

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.pavan.fragmentdemo.DetailFragment" />

</LinearLayout>

列表_fragment.xml的代码

<?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="horizontal" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detail_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="com.pavan.fragmentdemo.DetailFragment" >
    </fragment>

</LinearLayout>
<?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:background="#FFFF99"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/display_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="40sp" />

</LinearLayout>
<?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:background="#CCFF99"
    android:orientation="vertical"
    android:padding="5dp" >

    <Button
        android:id="@+id/android_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android" />

    <Button
        android:id="@+id/ios_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS" />

    <Button
        android:id="@+id/windows_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows" />

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.pavan.fragmentdemo.DetailFragment" />

</LinearLayout>

布局端口中的 代码活动_main.xml

<?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="horizontal" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detail_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="com.pavan.fragmentdemo.DetailFragment" >
    </fragment>

</LinearLayout>
<?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:background="#FFFF99"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/display_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="40sp" />

</LinearLayout>
<?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:background="#CCFF99"
    android:orientation="vertical"
    android:padding="5dp" >

    <Button
        android:id="@+id/android_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android" />

    <Button
        android:id="@+id/ios_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS" />

    <Button
        android:id="@+id/windows_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows" />

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.pavan.fragmentdemo.DetailFragment" />

</LinearLayout>

详细代码\u activity.xml

<?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="horizontal" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detail_Fragment"
        android:layout_width="0sp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="com.pavan.fragmentdemo.DetailFragment" >
    </fragment>

</LinearLayout>
<?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:background="#FFFF99"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/display_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="40sp" />

</LinearLayout>
<?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:background="#CCFF99"
    android:orientation="vertical"
    android:padding="5dp" >

    <Button
        android:id="@+id/android_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android" />

    <Button
        android:id="@+id/ios_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS" />

    <Button
        android:id="@+id/windows_btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows" />

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/list_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.pavan.fragmentdemo.MyListFragment" >
    </fragment>

</LinearLayout>
<?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="vertical" >

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.pavan.fragmentdemo.DetailFragment" />

</LinearLayout>

您有不同的片段名称: 在xml com.pavan.fragmentdemo.MyListFragment中 在java com.example.fragmentapp.MyListFragment中


修复程序包名称

您有不同的片段名称: 在xml com.pavan.fragmentdemo.MyListFragment中 在java com.example.fragmentapp.MyListFragment中


修复程序包名称

请您更简洁一点好吗?您在此处发布了数百行代码。请注意,寻求调试帮助的问题必须包括所需的行为以及在问题本身中重现问题所需的最短代码。请通过阅读了解如何改进您的问题。请您更简洁一点好吗?您在此处发布了数百行代码。请注意,寻求调试帮助的问题必须包括所需的行为以及在问题本身中重现问题所需的最短代码。请通过阅读了解如何改进您的问题。