2017-09-03 4 views
0

Ich versuche eine neue Aktivität aus einem Fragment zu instanziieren. Und ich laufe immer wieder in denselben Fehler.Xamarin Visual Studio 2017 Java.Lang.ClassNotFoundException: Klasse "BarcodeFragmentActivity" nicht gefunden

java.lang.ClassNotFoundException: Haben Sie nicht gefunden Klasse "BarcodeFragmentActivity" auf dem Pfad: DexPathList [[zip-Datei „/data/app/StockApp.StockApp-1/base.apk"],nativeLibraryDirectories=[/ Daten/app/StockApp.StockApp-1/lib/Arm,/System/Fake-Bibliotheken, /data/app/StockApp.StockApp-1/base.apk!/lib/armeabi-v7a,/system/lib,/vendor/lib]]

Ich verwende Visual Studio 2017 Version 15.3.3. Ich habe High und Low für eine Antwort auf dieses Problem gesucht, aber egal was ich versucht habe, bekomme ich immer noch den gleichen Fehler. Wenn ich versuche,

<intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 

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

zu addieren, löst ein Fehler aus, es konnte BarcodeFragment on build nicht finden.

Meine Android Manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="StockApp.StockApp" android:versionCode="1" 
android:versionName="1.0" android:installLocation="preferExternal"> 
    <uses-sdk android:minSdkVersion="23" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <application android:allowBackup="true" android:label="@string/app_name" 
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" 
android:supportsRtl="true"> 
    <activity android:name=".barcodeFragment" android:label="Barcode Fragment"></activity> 
    <activity android:name=".barcodeFragmentActivity" android:label="Activity Barcode" android:parentActivityName=".barcodeFragment"> 
     <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".barcodeFragmentActivity"></meta-data> 
    </activity> 
</application> 

Das Fragment die Aktivität von aufgerufen wird:

für BarcodeFragmentActivity
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android; 
using Android.App; 
using Android.Content; 
using Android.Content.PM; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 

using Android.Gms.Vision.Barcodes; 
using Android.Gms.Common.Apis; 

using Android.Support.V4.View; 
using Android.Support.V4.App; 
using Android.Support.V7.App; 

namespace StockApp 
{ 
    class BarcodeFragment : Android.Support.V4.App.Fragment, View.IOnClickListener 
    { 
     private CompoundButton autoFocus { get; set; } 
     private CompoundButton useFlash { get; set; } 
     private Button btnReadBarcode { get; set; } 

     private TextView statusMessage { get; set; } 
     private TextView barcodeValue { get; set; } 

     private static int RC_BARCODE_CAPTURE = 9001; 
     private static string TAG = "BarcodeMain"; 

     public BarcodeFragment() { } 

     public void OnClick(View v) 
     { 
      if (v.Id == Resource.Id.read_barcode) 
      { 

       Intent intent = new   Intent(Context.ApplicationContext,Activity.ClassLoader.LoadClass("barcodeFragmentActivity"));//("StockApp.StockApp", typeof(BarcodeFragmentActivity)); 
      //intent.SetClassName("StockApp.StockApp",); 
      intent.PutExtra("AutoFocus", autoFocus.Checked); 
      intent.PutExtra("UseFlash", useFlash.Checked); 

      if (intent.ResolveActivity(Activity.PackageManager) != null) 
      { 
       StartActivityForResult(intent, RC_BARCODE_CAPTURE); 
      } 

     } 
    } 

    public override void OnActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (requestCode == RC_BARCODE_CAPTURE) 
     { 
      if(resultCode == CommonStatusCodes.Success) 
      { 
       if(data != null) 
       { 
        // Barcode barcode = data.GetParcelableExtra(BarcodeFragmentActivity.BarcodeObject); 
       } 
      } 
     } 
    } 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.Inflate(Resource.Layout.Fragment_BarcodeMain, container, false); 
     statusMessage = view.FindViewById(Resource.Id.txtBarcodeStatus) as TextView; 
     barcodeValue = view.FindViewById(Resource.Id.txtBarcodeValue) as TextView; 

     autoFocus = view.FindViewById(Resource.Id.auto_focus) as CompoundButton; 
     useFlash = view.FindViewById(Resource.Id.use_flash) as CompoundButton; 

     view.FindViewById(Resource.Id.read_barcode).SetOnClickListener(this); 
     btnReadBarcode = view.FindViewById(Resource.Id.read_barcode) as Button; 



     return view; 
    } 

    public static BarcodeFragment newInstance() 
    { 
     BarcodeFragment fragment = new BarcodeFragment(); 
     Bundle args = new Bundle(); 

     fragment.Arguments = args; 
     return fragment; 
    } 
} 
}  

Mein Code:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.Content.PM; 

using Android.Gms.Vision.Barcodes; 
using Android.Gms.Common.Apis; 
using Android.Gms.Common; 
using Android.Gms.Vision; 

using Android.Hardware; 

using Android.Support.V4.View; 
using Android.Support.V4.App; 
using Android.Support.V7.App; 
using Android.Support.Design.Widget; 

using StockApp.UI; 
using StockApp.BarcodeReader; 


namespace StockApp 
{ 
    class BarcodeFragmentActivity : AppCompatActivity 
    { 
    private static string tag = "Barcode-Reader"; 
    private static int RC_HANDLE_GMS = 9001; 
    private static int RC_HANDLE_CAMERA_PERM = 2; 

    public static string AutoFocus = "AutoFocus"; 
    public static string UseFlash = "UseFlash"; 
    public static string BarcodeObject = "Barcode"; 


    private CameraSourcePreview<BarcodeGraphic> mPreview; 
    private Android.Gms.Vision.CameraSource mCameraSource; 
    private GraphicOverlay<BarcodeGraphic> mGraphicOverlay; 

    private ScaleGestureDetector scaleGestureDetector; 
    private GestureDetector getsureDetector; 

    private View layout; 
    private static BarcodeFragmentActivity thisInstance; 

    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     thisInstance = this; 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.Barcode_Capture); 

     mPreview = (CameraSourcePreview<BarcodeGraphic>)FindViewById(Resource.Id.preview); 
     mGraphicOverlay = (GraphicOverlay<BarcodeGraphic>)FindViewById(Resource.Id.graphicOverlay); 

     bool autoFocus = Intent.GetBooleanExtra(AutoFocus, false); 
     bool useFlash = Intent.GetBooleanExtra(UseFlash, false); 

     int rc = (int) ActivityCompat.CheckSelfPermission(this, Manifest.Permission.Camera); 
     if (rc == (int) Permission.Granted) 
     { 
      createCameraSource(autoFocus, useFlash); 
     } 
     else 
     { 
      requestCameraPermission(); 
     } 

     getsureDetector = new GestureDetector(this, new CaptureGestureListener()); 
    } 

    private void requestCameraPermission() 
    { 
     string[] permissions = new string[] { Manifest.Permission.CallPhone }; 
     if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.Camera)) 
     { 

      Snackbar.Make(layout, "Require Camera Permions To Read Barcodes", Snackbar.LengthIndefinite) 
       .SetAction("Ok", new Action<View> (delegate(View obj) 
       { 
        ActivityCompat.RequestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM); 
       } 
        )).Show(); 
     } 
     else 
     { 
      ActivityCompat.RequestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM); 
     } 

     //Activity thisActivity = this; 
     //View.IOnClickListener listener = new View.IOnClickListener; 

    } 

    public override bool OnTouchEvent(MotionEvent e) 
    { 
     bool b = scaleGestureDetector.OnTouchEvent(e); 
     bool c = getsureDetector.OnTouchEvent(e); 
     return b || c || base.OnTouchEvent(e); 
    } 

    private void createCameraSource(bool autoFocus, bool useFlash) 
    { 
     Context context = ApplicationContext; 

     BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).Build(); 
     BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay); 
     barcodeDetector.SetProcessor(
      new MultiProcessor.Builder(barcodeFactory).Build()); 

     if (!barcodeDetector.IsOperational) 
     { 
      IntentFilter lowstorageFilter = new IntentFilter(Intent.ActionDeviceStorageLow); 
      bool hasLowStorage = RegisterReceiver(null, lowstorageFilter) != null; 

      if (hasLowStorage) 
      { 
       Toast.MakeText(this, "Low Storage Error", ToastLength.Long); 
      } 
     } 

     Android.Gms.Vision.CameraSource.Builder builder = new Android.Gms.Vision.CameraSource.Builder(base.ApplicationContext, barcodeDetector) 
      .SetFacing(Android.Gms.Vision.CameraFacing.Back) 
      .SetRequestedPreviewSize(1600, 1024) 
      .SetRequestedFps(15.0f) 
      .SetAutoFocusEnabled(true); 

     mCameraSource = builder.Build(); 




    } 

    private void startCameraSource() 
    { 
     int code = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(ApplicationContext); 

     if(code != ConnectionResult.Success) 
     { 
      Dialog dig = GoogleApiAvailability.Instance.GetErrorDialog(this, code, RC_HANDLE_GMS); 
      dig.Show(); 
     } 

     if (mCameraSource != null) 
     { 
      try 
      { 
       mPreview.start(mCameraSource, mGraphicOverlay); 
      } 
      catch (InvalidOperationException) 
      { 
       mCameraSource.Release(); 
       mCameraSource = null; 
      } 
     } 
    } 

    private bool OnTap(float rawX, float rawY) 
    { 
     int[] location = new int[2]; 
     mGraphicOverlay.GetLocationOnScreen(location); 
     float x = (rawX - location[0]); 
     float y = (rawY - location[1]); 

     Barcode best = null; 
     float bestDistance = float.MaxValue; 

     foreach (BarcodeGraphic graphic in mGraphicOverlay.getGraphics()) 
     { 
      Barcode barcode = graphic.GetBarcode(); 

      if(barcode.BoundingBox.Contains((int)x,(int)y)) 
      { 
       best = barcode; 
       break; 
      } 

      float dx = x - barcode.BoundingBox.CenterX(); 
      float dy = y - barcode.BoundingBox.CenterY(); 

      float distance = (dx * dx) + (dy * dy); 

      if (distance > bestDistance) 
      { 
       best = barcode; 
       bestDistance = distance; 
      } 

      if (best != null) 
      { 
       Intent data = new Intent(); 
       data.PutExtra(BarcodeObject, best); 
       SetResult(CommonStatusCodes.Success, data); 
       Finish(); 
       return true; 
      } 

     } 
     return false; 

    } 

    protected override void OnResume() 
    { 
     base.OnResume(); 
     startCameraSource(); 
    } 

    protected override void OnPause() 
    { 
     base.OnPause(); 
     if (mPreview != null) 
     { 
      mPreview.stop(); 
     } 
    } 

    protected override void OnDestroy() 
    { 
     base.OnDestroy(); 
     if(mPreview != null) 
     { 
      mPreview.release(); 
     } 
    } 

    private class CaptureGestureListener : GestureDetector.SimpleOnGestureListener 
    { 
     public override bool OnSingleTapConfirmed(MotionEvent e) 
     { 
      return thisInstance.OnTap(e.RawX, e.RawY) || base.OnSingleTapConfirmed(e); 
     } 
    } 

    private class ScaleListener : ScaleGestureDetector.IOnScaleGestureListener 
    { 
     public IntPtr Handle => throw new NotImplementedException(); 

     public void Dispose() 
     { 
      throw new NotImplementedException(); 
     } 

     public bool OnScale(ScaleGestureDetector detector) 
     { 
      return false; 
     } 

     public bool OnScaleBegin(ScaleGestureDetector detector) 
     { 
      return true; 
     } 

     public void OnScaleEnd(ScaleGestureDetector detector) 
     { 
     } 
    } 

    } 
} 

Antwort

1

Ich versuche, eine neue Aktivität aus einem Fragment zu instanziieren.

In Ihrem Code, ich sehe dies:

Activity.ClassLoader.LoadClass("barcodeFragmentActivity")//("StockApp.StockApp", typeof(BarcodeFragmentActivity)); 

Warum verwenden Sie LoadClass Methode? In Ihrem Projekt verwenden Sie MultiDex und barcodeFragmentActivity in Ihrem anderen classes2.dex?

Wenn nicht, ändern Sie den Code aus:

Intent intent = new Intent(Context.ApplicationContext, Activity.ClassLoader.LoadClass("barcodeFragmentActivity")); 

Um

Intent intent = new Intent(Context.ApplicationContext, typeof(BarcodeFragmentActivity)); 

Es wird gut funktioniert.

+0

Dank habe ich den Code jedoch bearbeitet ich jetzt einen Fehler bekommen die Ausnahme barcodeFragmentActivity Unhandle zum Erstellen von: Android.Views.InflateException: Binary XML-Datei Zeile # 1: Binary XML-Datei Zeile # 1: Fehler aufblasen Klasse StockApp .StockApp.UI.CameraSourcePreview ist aufgetreten –

+0

@James Scotland, es scheint etwas falsch mit Ihrer 'CameraSourcePreview', Sie könnten eine neue Frage öffnen und fügen Sie bitte eine vollständige Diagnose Build-Fehlerprotokoll. –

Verwandte Themen