2017-07-18 7 views
0

主な活動XMLファイル私はフラグメントからMainActivityに取り込まれた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> 

主な活動JAVAファイル

package com.harsh1507.pc.t_1372; 

import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity implements Frag_a.comm 
{ 
    TextView tv; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv= (TextView) findViewById(R.id.tv); 
    } 

    @Override 
    public void com(double s) { 
     tv.setText("Result is : "+Double.toString(s)); 
    } 
} 

フラグメントを実行していません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> 

フラグメントJAVAファイル、この部分には主な問題があるかもしれませんが、私はできません私が何かを逃した場所を教えてください。

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); 
    } 
} 
+0

uが断片を追加するように実装されなければなりません? –

+0

@AvinashRoyフラグメントがメインアクティビティXMLファイルに追加されました。 –

+0

コンパイル時にエラーが出るのですか? –

答えて

0

urの主なアクティビティがAppCompatの代わりにFragmentActivityを拡張していることを確認してください。

対応インポートは次のようになります

import android.support.v4.app.FragmentActivity; 

断片を使用して、任意の活動はFragmentActivityのサブクラスの代わりに、伝統的なアクティビティクラス

+0

AppCompatはデフォルトでインポートされるので、削除する必要があります。他の機能が影響を受けるとは思わないでください。 –

+0

私はAppCompatの代わりにFragmentActivityを拡張しましたが、まだ停止しています。 –

関連する問題