从另一个片段的其他视图控制片段的视图

时间:2018-10-07 12:44:41

标签: android android-studio android-fragments

我正在写一个使用片段的简单示例。在主要活动中,有两个片段。第一个片段具有一些文本视图,第二个片段具有一些按钮。当用户单击文本视图时,按钮上的文本将被更改。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" android:background="@drawable/ic_launcher_background">

    <fragment
        android:id="@+id/MenuFrag"
        android:name="www.myapplication.MenuFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/ContentFrag"
        android:name="www.myapplication.ContentFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

MainActivity.java

package www.application;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements MenuFrag.IFragmentClickListener{

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

    @Override
    public void onMenuItemClick(int position) {
        ContentFrag content = (ContentFrag) getSupportFragmentManager().findFragmentById(R.id.ContentFrag);
        if(content!=null)
            content.updateContent(position);
    }
}

menu_frag.xml(第一个片段)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@color/colorPrimaryDark"
    tools:context=".MenuFrag">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 1"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 2"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 3"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 4"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 5"
            android:textSize="20sp"
            android:typeface="serif" />
    </LinearLayout>

</FrameLayout>

MenuFrag.java

package www.application;

import android.content.Context;
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;
import android.widget.TextView;

public class MenuFrag extends ListFragment {
    IFragmentClickListener itemfragment;
    TextView tv1;

    private View.OnClickListener onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                 itemfragment.onMenuItemClick(1);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_frag, container, false);
        tv1= view.findViewById(R.id.tv1);
        tv1.setOnClickListener(onclick);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            itemfragment = (IFragmentClickListener) context;
        }catch(ClassCastException e){e.printStackTrace();}
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        itemfragment.onMenuItemClick(position);
    }

    public interface IFragmentClickListener{
        public void onMenuItemClick(int position);
    }
}

content_frag.xml(第二个片段)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@color/colorPrimary"
    tools:context=".ContentFrag">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btmessage"/>
    </ScrollView>

</FrameLayout>

ContentFrag.java

package www.application;

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.Button;

public class ContentFrag extends Fragment {

    Button btmessage;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_frag, container, false);
        btmessage = v.findViewById(R.id.btmessage);
        return v;
    }

    public void updateContent(int position){
        btmessage.setText("You clicked !");
    }
}

我没有编写控制按钮的操作,只是生成了onclick事件。但是,我得到了错误。请帮助我修复错误,然后继续完成任务。

  

E / Android运行时:致命异常:主要                     流程:www.myapplication,PID:21117                     java.lang.RuntimeException:无法启动活动ComponentInfo {www.myapplication / www.myapplication.MainActivity}:   android.view.InflateException:二进制XML文件第10行:错误   膨胀类片段                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)                         在android.app.ActivityThread.access $ 800(ActivityThread.java:144)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:135)                         在android.app.ActivityThread.main(ActivityThread.java:5221)                         在java.lang.reflect.Method.invoke(本机方法)                         在java.lang.reflect.Method.invoke(Method.java:372)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)                      原因:android.view.InflateException:二进制XML文件行#10:错误夸大类片段                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:504)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:414)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:365)                         在android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)                         在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)                         在www.myapplication.MainActivity.onCreate(MainActivity.java:11)                         在android.app.Activity.performCreate(Activity.java:5937)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)                         在android.app.ActivityThread.access $ 800(ActivityThread.java:144)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:135)                         在android.app.ActivityThread.main(ActivityThread.java:5221)                         在java.lang.reflect.Method.invoke(本机方法)                         在java.lang.reflect.Method.invoke(Method.java:372)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)                      由以下原因引起的:java.lang.RuntimeException:您的内容必须具有ID属性为“ android.R.id.list”的ListView。                         在android.support.v4.app.ListFragment.ensureList(ListFragment.java:349)                         在android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:150)                         在android.support.v4.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1695)                         在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1431)                         在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684)                         在android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1930)                         在android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3745)                         在android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)                         在android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)                         在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:387)                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:504)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:414)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:365)                         在android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)                         在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)                         在www.myapplication.MainActivity.onCreate(MainActivity.java:11)                         在android.app.Activity.performCreate(Activity.java:5937)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)                         在android.app.ActivityThread.access $ 800(ActivityThread.java:144)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:135)                         在android.app.ActivityThread.main(ActivityThread.java:5221)                         在java.lang.reflect.Method.invoke(本机方法)                         在java.lang.reflect.Method.invoke(Method.java:372)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:2)

问题在于MenuFrag Fragment(初始化TextView的方式)。
tv1= getActivity().findViewById(R.id.tv1);

相反,我建议将其移至onCreateView()。 像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.menu_frag, container, false);
    tv1 = view.findViewById(R.id.tv1);
    tv1.setOnClickListener(onclick);
    return view;
} 

调用getActivity().findViewById时,它将在活动的布局中搜索找不到的TextView ID。