我只是用一个片段替换其他片段,但无法解决为什么它不起作用

时间:2016-01-09 23:00:51

标签: android fragmenttransaction

我只是为片段的事务编写代码。但是我收到了这个错误。我是android新手,我无法解决此错误。 然而它正在与我的旧Android工作室合作。我认为我的进口问题。请你帮助我好吗?我认为不需要我的片段文件。它仅仅是onCreateView方法。

它不是替换片段。 在这里我的    MainActivity.java 包com.example.pkkeshar.tallymaster;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView transaction;
TextView summary;
TextView details;
FragmentManager fm;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    transaction = (TextView) findViewById(R.id.transaction);
    summary = (TextView) findViewById(R.id.details);
    details = (TextView) findViewById(R.id.summary);



    transaction.setOnClickListener(this);
    summary.setOnClickListener(this);
    details.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if(v==findViewById(R.id.details)){
      Fragment fragment = new details();
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.linearLayout1, fragment);
        transaction.commit();
    }

    if(v==findViewById(R.id.summary)){
        Fragment fragment = new summary();
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.linearLayout1, fragment);
        transaction.commit();
    }

    if(v==findViewById(R.id.transaction)){
        Fragment fragment = new transaction();
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.linearLayout1, fragment);
        transaction.commit();
    }
}
}

这里是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.pkkeshar.tallymaster.MainActivity">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3"
    android:paddingBottom="5dp"
    android:id="@+id/linearLayout">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="Transaction"
        android:id="@+id/transaction"
        android:layout_weight="1"
        android:textSize="20dp"
        android:gravity="center_horizontal" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="Details"
        android:id="@+id/details"
        android:layout_weight="1"
        android:textSize="20dp"
        android:gravity="center_horizontal" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="Summary"
        android:id="@+id/summary"
        android:layout_weight="1"
        android:textSize="20dp"
        android:gravity="center_horizontal" />
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/linearLayout"
    android:id="@+id/linearLayout1">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.pkkeshar.tallymaster.transaction"
        android:id="@+id/fragment"
        tools:layout="@layout/fragment_transaction" />
</LinearLayout>

0 个答案:

没有答案