Java 我试图通过xml添加fragment,并将从fragment获取的数据打印到MainActivity,但代码不是';t运行

Java 我试图通过xml添加fragment,并将从fragment获取的数据打印到MainActivity,但代码不是';t运行,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,主活动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:l

主活动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="vertical"
    tools:context="com.harsh1507.pc.t_1372.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:id="@+id/l1">

        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.harsh1507.pc.t_1372.Frag_a"/>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/tv"/>

</LinearLayout>
片段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="match_parent"
    android:orientation="horizontal"
    tools:context="com.harsh1507.pc.t_1372.Frag_a">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:inputType="number"
            android:id="@+id/ed1"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/ed2"
            android:inputType="number"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="cal"
            android:id="@+id/btn"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/rg"
            android:orientation="vertical">

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="ADD"
                android:checked="true"
                android:id="@+id/rb1"/>

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="SUBSTRACT"
                android:id="@+id/rb2"/>

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="MULTIPLY"
                android:id="@+id/rb3"/>

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="DIVIDE"
                android:id="@+id/rb4"/>

        </RadioGroup>

    </LinearLayout>
</LinearLayout>

确保您的主活动扩展FragmentActivity而不是AppCompat

相应的进口将是:

import android.support.v4.app.FragmentActivity;

任何使用片段的活动都必须作为FragmentActivity的子类来实现,而不是传统的活动类

在何处添加片段???@AvinashRoy片段添加到主活动XML文件中。它是否会导致编译时错误?这可能是使用v4和v7支持LIB造成的问题吗?尝试将主活动设置为(v4)FragmentActivity。@SagarPujari应用程序在启动前在我的手机中停止。默认情况下,AppCompat已导入,因此我应该删除它,您不认为其他功能会受到影响。我扩展了FragmentActivity而不是AppCompat,但它仍在停止。
package com.harsh1507.pc.t_1372;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Frag_a extends Fragment
{
    View v;
    comm c;
    EditText ed1,ed2;
    Button btn;
    RadioGroup rg;
    RadioButton rb1,rb2,rb3,rb4;
    double d1,d2,comp;

    @Override
    public void onAttach(Context context) 
    {
        super.onAttach(context);
        c=(comm) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        v= inflater.inflate(R.layout.fragment_frag_a, container, false);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);

        ed1=v.findViewById(R.id.ed1);
        ed2=v.findViewById(R.id.ed2);
        btn=v.findViewById(R.id.btn);
        rb1=v.findViewById(R.id.rb1);
        rb2=v.findViewById(R.id.rb2);
        rb3=v.findViewById(R.id.rb3);
        rb4=v.findViewById(R.id.rb4);
        rg= v.findViewById(R.id.rg);
        d1=Double.parseDouble(ed1.getText().toString());
        d2=Double.parseDouble(ed2.getText().toString());

        btn.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View view) {
                if (rb1.isChecked()) {
                    comp = d1 + d2;
                    c.com(comp);
                }
                if (rb2.isChecked()) {
                    comp = d1 - d2;
                    c.com(comp);
                }
                if (rb3.isChecked()) {
                    comp = d1 * d2;
                    c.com(comp);
                }
                if (rb4.isChecked()) {
                    comp = d1 / d2;
                    c.com(comp);
                }
            }
        });
    }

    public interface comm{
        void com(double s);
    }
}
import android.support.v4.app.FragmentActivity;