我试图从片段
调用ZXingScannerActivityNewInventoryFragement.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
中添加了活动 <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是扩展活动并让导航抽屉导航多个片段。
库存是该片段之一,带有添加新库存的按钮意味着导航到NewInventoryFragment。
NewInventoryFragment有这个@line#46 getActivity()。startActivityForResult(intent,ZXING_SCANNER_REQUEST);
这样做是否正确?
答案 0 :(得分:1)
您无法在Fragment上处理此事件,因为它是从自己的活动处理的。
我的意思是,片段没有上下文,因为它们是活动的一部分。你可以在上下文中处理事件,所以,你必须在包含片段的活动中编写这种方法。