无法从项目单击启动片段

时间:2017-03-20 22:10:55

标签: 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.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>

片段java:

import android.support.v4.app.Fragment;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.support.constraint.ConstraintLayout;
import android.widget.TextView;
import android.widget.Button;

public class CheckBalance extends Fragment implements View.OnClickListener {

    private LinearLayout africellCbInt;
    private ConstraintLayout africellCbBalanceWrapper;
    private TextView africellCbDialogMsg;
    private TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.custom_africell_cb, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        africellCbInt = (LinearLayout) view.findViewById(R.id.africell_cb_int);
        africellCbDialogMsg = (TextView) view.findViewById(R.id.africell_cb_dialog_msg);
        view.findViewById(R.id.button_africell_cb_1).setOnClickListener(this);
        textView = (TextView) view.findViewById(R.id.textView);
        view.findViewById(R.id.button_africell_cb_2).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_africell_cb_1:
                //bla bla bla
                break;
            case R.id.button_africell_cb_2:
                //bla bla bla
                break;
        }
    }
}

片段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>

拜托,有人可以告诉我有什么问题吗?我知道我错过了一些东西,但无法弄清楚。

编辑: logcat的:

    03-20 23:41:40.768 17618-17618/com.esqmo.apps.mosungiplus E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.esqmo.apps.mosungiplus, PID: 17618
                                                                            java.lang.IllegalArgumentException: Expected receiver of type com.esqmo.apps.mosungiplus.AccountActivity, but got android.view.ContextThemeWrapper
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at android.view.View$DeclaredOnClickListener.onClick(View.java:4735)
                                                                                at android.view.View.performClick(View.java:5697)
                                                                                at android.view.View$PerformClick.run(View.java:22526)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:158)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
03-20 23:41:40.798 855-17790/? E/android.os.Debug: ro.product_ship = true
03-20 23:41:40.798 855-17790/? E/android.os.Debug: ro.debug_level = 0x4f4c
03-20 23:41:40.798 855-17790/? E/android.os.Debug: sys.mobilecare.preload = false
03-20 23:41:52.718 855-1676/? E/LocSvc_ApiV02: I/<--- void globalRespCb(locClientHandleType, uint32_t, locClientRespIndUnionType, void*) line 131 QMI_LOC_INJECT_POSITION_REQ_V02
03-20 23:41:55.648 327-1200/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'LABEL' not found
03-20 23:41:55.648 327-1200/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'TIME_NS' not found
03-20 23:41:55.648 327-1200/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'UID' not found
03-20 23:41:55.958 1549-1590/? E/ContactsProvider_EventLog: Flush buffer to file cnt : 10 size : 4Kb duration : 203ms lastUpdatedAfter : 180640ms
03-20 23:42:04.158 855-1597/? E/Watchdog: !@Sync 2468 [03-20 23:42:04.161]
03-20 23:42:23.948 327-1200/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'LABEL' not found
03-20 23:42:23.948 327-1200/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'TIME_NS' not found
03-20 23:42:23.948 327-1200/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'UID' not found
03-20 23:42:32.398 18642-18642/? E/Zygote: v2
03-20 23:42:32.408 18642-18642/? E/Zygote: accessInfo : 0
03-20 23:42:34.158 855-1597/? E/Watchdog: !@Sync 2469 [03-20 23:42:34.163]

究竟是什么错误?

java.lang.IllegalArgumentException: Expected receiver of type com.esqmo.apps.mosungiplus.AccountActivity, but got android.view.ContextThemeWrapper at java.lang.reflect.Method.invoke(Native Method) at android.view.View$DeclaredOnClickListener.onClick(View.java:4735) 

1 个答案:

答案 0 :(得分:1)

这一次尝试将onClicklistener设置为RelativeLayout in onCreate

首先,从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();
 }