gpt4 book ai didi

android - 从 fragment 调用 startActivityForResult 时出错

转载 作者:行者123 更新时间:2023-11-30 02:13:21 25 4
gpt4 key购买 nike

我正在尝试从 fragment 调用 ZXingScannerActivity

NewInventoryFragement.java

package info.androidhive.wolf;

import android.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import info.androidhive.wolf.qrcode.ZXingScannerActivity;
import info.androidhive.wolf.qrcode.ZXingConstants;
import com.google.zxing.BarcodeFormat;

/**
* Created by Admin on 4/22/2015.
*/
public class NewInventoryFragment extends Fragment{

private static final int ZXING_SCANNER_REQUEST = 0;
private static final int ZXING_QR_SCANNER_REQUEST = 1;
Button button;
public NewInventoryFragment(){};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_inventory_fragment, container, false);

button=(Button) rootView.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
launchQRScanner(view);
}
});
return rootView;
}

public void launchQRScanner(View v) {
if (isCameraAvailable()) {
Intent intent = new Intent(getActivity().getApplicationContext(), ZXingScannerActivity.class);
intent.putExtra(ZXingConstants.SCAN_FORMATS, BarcodeFormat.QR_CODE.toString());
getActivity().startActivityForResult(intent, ZXING_SCANNER_REQUEST);
} else {
Toast.makeText(getActivity().getApplicationContext(), "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
}
}

public boolean isCameraAvailable() {
PackageManager pm = getActivity().getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ZXING_SCANNER_REQUEST:
case ZXING_QR_SCANNER_REQUEST:
if (resultCode == 1) {
Toast.makeText(getActivity().getApplicationContext(), "Scan Result = " + data.getStringExtra(ZXingConstants.SCAN_RESULT) +
", Scan Format = " + data.getStringExtra(ZXingConstants.SCAN_RESULT_FORMAT), Toast.LENGTH_SHORT).show();

}
break;
}
}

}

Logcat 说

26260-26260/info.androidhive.slidingmenu E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {info.androidhive.slidingmenu/info.androidhive.wolf.qrcode.ZXingScannerActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1556)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1530)
at android.app.Activity.startActivityFromFragment(Activity.java:3922)
at android.app.Fragment.startActivityForResult(Fragment.java:1025)
at android.app.Fragment.startActivityForResult(Fragment.java:1009)
at info.androidhive.wolf.NewInventoryFragment.launchQRScanner(NewInventoryFragment.java:46)
at info.androidhive.wolf.NewInventoryFragment$1.onClick(NewInventoryFragment.java:36)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17343)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)

@line #46 getActivity().startActivityForResult(intent, ZXING_SCANNER_REQUEST);我从 Stack 中引用了这个

我在Manifest.xml 中添加了 Activity

   <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="info.androidhive.wolf.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="info.androidhive.wolf.qrcode.ZXingScannerActivity"
android:screenOrientation="landscape"
android:label="@string/app_name"
/>


</application>

实际上,我在这里所做的是 MainActivity.java 是扩展 Activity 并具有抽屉导航以使用多个 fragment 进行导航。

Inventory 是带有添加新库存按钮的 fragment 之一,意味着导航到 NewInventoryFragment。

NewInventoryFragment 具有此@line #46 getActivity().startActivityForResult(intent, ZXING_SCANNER_REQUEST);

这是正确的做法吗??

最佳答案

你不能在 Fragment 上处理这个事件,因为它是从它自己的 Activity 中处理的。

我的意思是, fragment 没有上下文,因为它们是 Activity 的一部分。有些事件只能在上下文中处理,因此,您必须在包含 fragment 的 Activity 中编写此类方法。

关于android - 从 fragment 调用 startActivityForResult 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29795700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com