Android fragments 无法从单击的项目启动片段

Android fragments 无法从单击的项目启动片段,android-fragments,onclick,Android Fragments,Onclick,我试图在按下一个项目时启动一个片段,但应用程序崩溃了 这是我的代码: java活动: import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout; import android.widget.FrameLayout; import android.support.v4.app.Fra

我试图在按下一个项目时启动一个片段,但应用程序崩溃了

这是我的代码:

java活动:

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


public class AccountActivity extends AppCompatActivity {

    private String carrierName;


    private RelativeLayout lcb;
    private RelativeLayout layoutBalanceLoad;
    private RelativeLayout layoutCreditShare;
    private RelativeLayout layoutCreditBarrow;
    private FrameLayout dashboardProgressBar;

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


        lcb = (RelativeLayout) findViewById(R.id.layout_balance_check);
        layoutBalanceLoad = (RelativeLayout) findViewById(R.id.layout_balance_load);
        layoutCreditShare = (RelativeLayout) findViewById(R.id.layout_credit_share);
        layoutCreditBarrow = (RelativeLayout) findViewById(R.id.layout_credit_barrow);
        dashboardProgressBar = (FrameLayout) findViewById(R.id.dashboard_progress_bar);


    }

    public void checkBalance(View view) {

            Fragment fragment = new CheckBalance();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, fragment).commitAllowingStateLoss();


    }
}
活动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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@id/layout_dashboard_parent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:id="@id/main_scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:orientation="vertical"
            android:id="@id/layout_dashboard_scroll_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <FrameLayout
                android:id="@id/dashboard_alert_section"
                android:visibility="gone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <include layout="@layout/dashboard_feature_tip_item" />
            </FrameLayout>

            <LinearLayout
                android:id="@id/main_control_layout"
                android:layout_width="fill_parent"
                android:layout_height="611dp">

                <LinearLayout
                    android:id="@id/layout_main"
                    style="@style/CardView.Dark"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/c_dashboard_btn_quick_clean_color"
                    android:gravity="bottom"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:id="@+id/layout_balance_check"
                        android:layout_width="fill_parent"
                        android:layout_height="@dimen/dashboard_feature_layout_height_usa"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginTop="@dimen/dashboard_feature_top_margin_usa"
                        android:layout_weight="1.0"
                        android:background="@color/battery_charging_icon_color"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:onClick="checkBalance"
                        android:theme="@style/Base.Theme.AppCompat"
                        tools:targetApi="lollipop">

                        <include layout="@layout/sub_layout_account_cb" />

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/layout_balance_load"
                        android:layout_width="fill_parent"
                        android:layout_height="@dimen/dashboard_feature_layout_height_usa"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_weight="1.0"
                        android:background="@color/white"
                        android:clickable="true"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:focusable="true"
                        tools:targetApi="lollipop">

                        <include layout="@layout/sub_layout_account_lb" />

                        <View
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:background="?android:selectableItemBackground"
                            android:duplicateParentState="true" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/layout_credit_share"
                        android:layout_width="fill_parent"
                        android:layout_height="@dimen/dashboard_feature_layout_height_usa"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_weight="1.0"
                        android:background="@color/white"
                        android:clickable="true"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:focusable="true"
                        tools:targetApi="lollipop">

                        <include layout="@layout/sub_layout_account_sc" />

                        <View
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:background="?android:selectableItemBackground"
                            android:duplicateParentState="true" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/layout_credit_barrow"
                        android:layout_width="fill_parent"
                        android:layout_height="@dimen/dashboard_feature_layout_height_usa"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:background="@color/white"
                        android:clickable="true"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:focusable="true"
                        android:visibility="visible"
                        tools:targetApi="lollipop">

                        <include layout="@layout/sub_layout_account_bc" />

                        <View
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:background="?android:selectableItemBackground"
                            android:duplicateParentState="true" />
                    </RelativeLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="10dip"
        android:layout_gravity="bottom"
        android:elevation="5dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        tools:targetApi="lollipop" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

    </FrameLayout>


</RelativeLayout>
片段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:id="@+id/africell_cb_int"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/c_main_back_ground_color"
    android:orientation="vertical"
    android:padding="10.0dip"
    android:weightSum="1.0">


    <LinearLayout
        android:layout_width="344dp"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/africell_cb_dialog_msg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:layout_marginBottom="50dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:gravity="top|center"
            android:text="@string/africell_cb_dialog_msg"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/button_africell_cb_1"
            app:layout_constraintHorizontal_bias="0.507"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.486" />
    </LinearLayout>

    <Button
        android:id="@+id/button_africell_cb_1"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="328dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@color/c_dashboard_btn_quick_clean_color"
        android:elevation="6dp"
        android:onClick="mainBalance"
        android:text="@string/africell_cb_1"
        android:textColor="@color/action_button_color"
        android:visibility="visible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_editor_absoluteY="39dp"
        tools:targetApi="lollipop" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="5dip"
        android:layout_marginBottom="2dip"
        android:layout_marginTop="2dip"
        android:background="@color/white"
        android:text="TextView"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button_africell_cb_2"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="327dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@color/c_dashboard_btn_quick_clean_color"
        android:elevation="9dp"
        android:text="@string/africell_cb_2"
        android:textColor="@color/action_button_color"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:targetApi="lollipop" />


</LinearLayout>

在onCreate中,尝试将
onClicklistener
设置为您的
RelativeLayout
,这次:

首先,从xml代码中删除android:onClick=“checkBalance”
。然后按以下步骤操作:

lcb.setOnClickListener(new View.OnClickListener(){          
            @Override
            public void onClick(View view) {
                 Fragment fragment = new CheckBalance();
                 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                 transaction.replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
            }                
        });
要删除“后退”按钮上的碎片,请首先全局声明
FragmentManager

 FragmentManager fm;
接下来,替换您的以下代码:

 Fragment fragment = new CheckBalance();
 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 transaction.replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
为此:

 Fragment fragment = new CheckBalance();
 fm = getSupportFragmentManager();
 FragmentTransaction ft = fm.beginTransaction();
 ft.addToBackStack("Your Fragment Tag");
 ft.add(R.id.fragment_container, fragment);
 ft.commit();
最后,覆盖活动中的
onBackPressed()

 @Override
 public void onBackPressed() {
     if (fm.getBackStackEntryCount() > 0)
        fm.popBackStackImmediate();
     else super.onBackPressed();
 }

你面临什么样的错误?请添加logcat。@tahsinRupam检查编辑,谢谢。不要用xml定义单击事件,而是用java中的
setOnClickListener()
方法尝试它。对不起,我去睡觉了。检查我下面的答案。总是一样的。片段未启动。您是否删除了xml中的android:onClick=“checkBalance”?具体是什么:
java.lang.IllegalArgumentException:预期的接收方类型为com.esqmo.apps.mosungiplus.AccountActivity,但在java.lang.reflect.Method.invoke(本机方法)处获得了android.view.contexthemewrapper在android.view.view$DeclaredOnClickListener.onClick(view.java:4735)
上成功了!我找到了一种方法,可以让后退按钮关闭碎片,而不必按下后退按钮OK,谢谢。非常感谢你的帮助。
 Fragment fragment = new CheckBalance();
 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 transaction.replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
 Fragment fragment = new CheckBalance();
 fm = getSupportFragmentManager();
 FragmentTransaction ft = fm.beginTransaction();
 ft.addToBackStack("Your Fragment Tag");
 ft.add(R.id.fragment_container, fragment);
 ft.commit();
 @Override
 public void onBackPressed() {
     if (fm.getBackStackEntryCount() > 0)
        fm.popBackStackImmediate();
     else super.onBackPressed();
 }