2013-02-07 37 views
0

我正在創建一個Android應用程序,它使用Microsoft夏威夷SDK來執行手機相機拍攝的照片的OCR。我在執行我的Android應用程序時出錯。 日誌貓是這樣的:NoClassDefFoundError。如何解決它?

02-07 14:48:59.617: E/AndroidRuntime(24660): FATAL EXCEPTION: main 
02-07 14:48:59.617: E/AndroidRuntime(24660): java.lang.NoClassDefFoundError: com.example.mobile.RecognitionActivity 
02-07 14:48:59.617: E/AndroidRuntime(24660): at com.example.mobile.FotocameraActivity.onActivityResult(FotocameraActivity.java:143) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.Activity.dispatchActivityResult(Activity.java:4820) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2435) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2476) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1990) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3355) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.access$700(ActivityThread.java:127) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1155) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.os.Looper.loop(Looper.java:137) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.main(ActivityThread.java:4476) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at java.lang.reflect.Method.invokeNative(Native Method) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at java.lang.reflect.Method.invoke(Method.java:511) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583) 
02-07 14:48:59.617: E/AndroidRuntime(24660): at dalvik.system.NativeStart.main(Native Method) 

FotocameraActivity.Java的代碼是這樣的:

package com.example.mobile; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.location.GpsStatus; 
import android.location.GpsStatus.Listener; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.util.Log; 

import android.view.View; 
import android.widget.ImageView; 

public class FotocameraActivity extends Activity { 


    // dati che servono per la fotocamera 
private static final int CAMERA_REQUEST = 100; // un numero a nostro 
                           // piacimento 
File tmpFotoFile = null; 
byte[] bitmapdata; 
ImageView preview; 
LocationManager locationManager; 
String gps; 
final static String ARRAY_BYTE = "ARRAY_BYTE"; 
final static String GPS = "GPS"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
     startGpsTracking(); 
     } 

     try { 
       launchCamera(); 
     } catch (IOException e) { 
       throw new RuntimeException(e); 
     } 
} 

private void startGpsTracking() { 
     // TODO Auto-generated method stub 
     locationManager.addGpsStatusListener(gpsListener); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 3, myLocationListener); 
    } 

private Listener gpsListener = new Listener(){ 
    public void onGpsStatusChanged(int status){ 
     switch(status){ 
     case GpsStatus.GPS_EVENT_FIRST_FIX: 
       Log.d(LOCATION_SERVICE,"onGpsStatusChanged First Fix"); 
       break; 
     case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
       Log.d(LOCATION_SERVICE, "onGpsStatusChanged Satellite"); 
       break; 
     case GpsStatus.GPS_EVENT_STARTED: 
       Log.d(LOCATION_SERVICE, "onGpsStatusChanged Started"); 
       break; 
     case GpsStatus.GPS_EVENT_STOPPED: 
       Log.d(LOCATION_SERVICE, "onGpsStatusChanged Stopped"); 
       break; 
     } 
    } 
}; 

private LocationListener myLocationListener = new LocationListener(){ 

     public void onLocationChanged(Location location) { 
       // TODO Auto-generated method stub 
       double lng = location.getLongitude(), lat = location.getLatitude(); 
       gps += String.format("%6f", lng) + "#" + String.format("%6f", lat); 

     } 

     public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 

     } 

     public void onProviderEnabled(String provider) { 
       // TODO Auto-generated method stub 

     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
       // TODO Auto-generated method stub 

     } 

}; 

    private void launchCamera() throws IOException { 

     // Fase 1 
     tmpFotoFile = File.createTempFile("OCRPic", null); 

     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //Uri.fromFile(tmpFotoFile)); 
     startActivityForResult(cameraIntent, CAMERA_REQUEST); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (requestCode == CAMERA_REQUEST) { 
       Bitmap datifoto = null; 
       Uri picUri = data.getData(); 
       if(picUri != null){ 

         try { 
           datifoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri); 
           //datifoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//Uri.fromFile(tmpFotoFile)); 
           } catch (FileNotFoundException e) { 
            throw new RuntimeException(e); 
           } catch (IOException e) { 
            throw new RuntimeException(e); 
           } 
       } 
         ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
         datifoto.compress(Bitmap.CompressFormat.JPEG, 70, bos); 
         bitmapdata = bos.toByteArray(); 
         //tmpFotoFile.delete(); 

       SimpleView view = new SimpleView(this, datifoto); // creo l'istanza 
                                 // // della                          // view... 
       setContentView(view); // e la setto 

       Intent intentRecognize = new Intent(this, RecognitionActivity.class); **<-Here refers the logcat for the error** 
       intentRecognize.putExtra(ARRAY_BYTE, bitmapdata); 
       intentRecognize.putExtra(GPS, gps); 
       startActivity(intentRecognize); 
     } 

} 
    } 


class SimpleView extends View{ 

private Bitmap bitmap; 
private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

public SimpleView(Context context, Bitmap bitmap) { 
     super(context); 
     this.bitmap = Bitmap.createScaledBitmap(bitmap,480,320,false);//ridimensiono l'immagine 
} 

@SuppressLint("DrawAllocation") 
@Override 
protected void onDraw(Canvas canvas){ 
super.onDraw(canvas); 
canvas.drawBitmap(bitmap, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint); 
} 


} 

雖然RecognitionActivity.Java的代碼是這樣的:

package com.example.mobile; 

import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.TextView; 
import microsoft.hawaii.sampleappbase.HawaiiBaseAuthActivity; 


public class RecognitionActivity extends HawaiiBaseAuthActivity { 

private TextView tv; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_recognition); 

    tv.setText("Prova"); 
    tv.setVisibility(View.VISIBLE); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_recognition, menu); 
    return true; 
} 

} 

我注意到,在RecognitionActivity中,如果我從Activity擴展該活動,它將起作用,但如果我擴展HawaiiBaseAuthActivity,則會崩潰。

我注意到在logcat的另一條消息,顯示只是在一瞬間,該應用程序啓動:

02-07 14:48:14.717: E/dalvikvm(24660): Could not find class 'com.example.mobile.RecognitionActivity', referenced from method com.example.mobile.FotocameraActivity.onActivityResult 

但該活動是我的工作區定義!

如果有人能幫助我,我將非常感激。

+0

這兩個在同一個項目中? – njzk2

回答

3

您是否已將com.example.mobile.RecognitionActivity添加到AndroidManifest.xml?

+0

是的,它的結果添加到清單! <活動 機器人:名字=「com.example.mobile.RecognitionActivity」 機器人:標籤=「@字符串/ title_activity_recognition」> –

+0

然後清理你的項目,然後重新建立,似乎在最終的apk這個生成類沒有打包。 – PaNaVTEC

+0

同樣的評論作爲@akmsharma – njzk2

0

好像你還沒有在清單文件中註冊「RecognitionActivity」活動。

+0

是的,它的結果添加到清單!

+0

不在manifest中的活動給出了一個非常不同的例外。無關的答案。 – njzk2

0
  1. 在清單文件中提及您的活動名稱和動作,以及它也是類別。
  2. 在您的活動中提及您的首發內容視圖並在活動中提及您的視圖ID。
相關問題