在onActivityResult

时间:2017-01-18 15:43:01

标签: android android-fragments

我有一个片段,其中3个按钮附加到名为MainActivity的活动类。当用户点击第一个按钮时,使用startActivityForResult()调用另一个活动。在第二个活动中,用户可以选择一些显示的文件。选择后,一旦用户点击Done,用户将被带回3个按钮的原始片段 我在super.onActivityResult(requestCode, resultCode, data);中呼叫MainActivity,以便将呼叫转移到片段的onActivityResult()

在片段onActivityResult()中,我收到所有选定的文件。我压缩它们并计算这个zip文件的大小。如果尺寸大于预定义尺寸,我会向用户显示提醒,当用户点击OK时,我会再次使用startActivityForResult()调用文件选择活动。

但是在onClick的{​​{1}}事件中,我得到一个例外AlertDialog。片段渲染得很好,为什么它没有附加到活动?

以下是相关的代码段:

片段fragment not attached to activity

onActivityResult

此方法启动文件浏览器活动:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("HF", "OAR");
    if (requestCode == SELECT_FILES) {    
        ... code to zip the file

        int size = getFileSize();
        if (fileName != null && new File(fileName).length() > size) {
            new AlertDialog.Builder(getActivity())
                .setTitle("Error")
                .setMessage("The message")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (isAdded())
                            startFileBrowser();
                        else {
                            Log.d("HF", "not attached");
                        }
                    }
                })
                .show();
        }
    }
}
片段中按钮的

private void startFileBrowser() { Intent intent = new Intent(activity, FileBrowserActivity.class); startActivityForResult(intent, SELECT_FILES); }

ClickListener

片段中的selectFilesButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (commonMethods.shouldBeActive(R.id.select_printer_id)) { new AlertDialog.Builder(getActivity()) .setTitle(getString(R.string.file_replace_title)) .setMessage(getString(R.string.file_replace_message)) .setIcon(android.R.drawable.ic_dialog_alert) .setPositiveButton(R.string.continue_string, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startFileBrowser(); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } else startFileBrowser(); } }); 方法:

onAttach

例外的Stacktrace:

private Activity activity;
@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof Activity){
        Log.d("HF", "OA");
        activity = (Activity) context;
    }
}

activity_main.xml中

E/UncaughtException: java.lang.IllegalStateException: Fragment HomeFragment{19909} not attached to Activity
    at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:952)
    at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:943)
    at com.package.HomeFragment.startFileBrowser(HomeFragment.java:305)
    at com.package.HomeFragment.access$000(HomeFragment.java:45)
    at com.package.HomeFragment$1.onClick(HomeFragment.java:119)
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=HomeActivity, _si=4833782661551315669, timestamp=1484755442671, fatal=1}]
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment HomeFragment{19909} not attached to Activity
    at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:952)
    at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:943)
    at com.package.HomeFragment.startFileBrowser(HomeFragment.java:305)
    at com.package.HomeFragment.access$000(HomeFragment.java:45)
    at com.package.HomeFragment$1.onClick(HomeFragment.java:119)
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

在“MainActivity”中设置片段的方法

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- The ActionBar -->
        <include
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:itemTextColor="?android:textColorPrimary"
        app:menu="@menu/nav_drawer_menu" />

</android.support.v4.widget.DrawerLayout>

我在private void setupFragment(Class fragmentClass, MenuItem menuItem) { try { Fragment fragment = (Fragment) fragmentClass.newInstance(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); menuItem.setChecked(true); setTitle(menuItem.getTitle()); drawerLayout.closeDrawers(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } }

中这样称呼它
onCreate()

0 个答案:

没有答案