1

I am trying to call ZXingScannerActivity from fragment

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 says

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); I have referenced this from Stack

I have added activities in 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>

Actually what i have done here is MainActivity.java is extends activity and having navigation drawer to navigate with multiple fragments.

Inventory is one of that fragment with button to add new inventory means navigate to NewInventoryFragment.

NewInventoryFragment having this @line #46 getActivity().startActivityForResult(intent, ZXING_SCANNER_REQUEST);

Is it correct way of doing this??

4
  • it's not related but don't use getActivity(). to start the activity, otherwise onActivityResult in your fragment will not be called Commented Apr 22, 2015 at 11:15
  • 1
    Where is info.androidhive.slidingmenu package ? Commented Apr 22, 2015 at 11:15
  • 1
    Which package is your ZXingScannerActivity.class located exactly? I think there is some issue with the full path to the Activity class. DO you have two classes with that name in your project? Commented Apr 22, 2015 at 11:25
  • No . classes imported correctly @Arnab Commented Apr 22, 2015 at 11:43

1 Answer 1

1

You can not handle this event on Fragment, because is handled from its own activity.

I mean, fragments doesn't have context, because are parts of an activity. there are events that you just can handle on a context, So, you have to write this kind of methods inside of the activity that contain the fragment.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response @Alejandro Lora is there way to implement navigation drawer with multiple activities always showing drawer on all activities
No, just one of course, when you implement a navigation drawer, you work with 1 activity and several fragments, but you can see the navigation drawer in all places because you are using the same activity.
ok,then how can i start activity from fragment @Alejandro Lora
Start another activity? it is unnecessary, for doing what? I think you don't understand how work navigation drawer, So I recommend you read this: developer.android.com/training/implementing-navigation/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.