2017-01-24 91 views
-1

我在mainactivity以下代碼:錯誤:致命異常:主無法啓動活動ComponentInfo

public class MainActivity extends Activity implements OnClickListener { 

    private boolean mIsFailed = false; 
    private Preview mPreview; 
    private ProcessImageAndDrawResults mDraw; 
    ........... 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 

      // Camera layer and drawing layer 
      mDraw = new ProcessImageAndDrawResults(this); 
      mPreview = new Preview(this, mDraw); 
      mDraw.mTracker = new HTracker(); 
..... 
      this.getWindow().setBackgroundDrawable(new ColorDrawable()); //black background 

      setContentView(mPreview); //creates MainActivity contents 
      addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

      // Menu 
      LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View buttons = inflater.inflate(R.layout.bottom_menu, null); 
      buttons.findViewById(R.id.ManualButton).setOnClickListener(this); 
      addContentView(buttons, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

     } 
    } 

    @Override 
    public void onClick(View view) { 
     if (view.getId() == R.id.ManualButton){ 
.................. 
     } 
    } 

    @Override 
    public void onPause() { 
...... } 

    @Override 
    public void onResume() { 
     ........} 
...... 
} 

class FaceRectangle { 
    public int x1, y1, x2, y2; 
} 

// Draw graphics on top of the video 
class ProcessImageAndDrawResults extends View { 
    public HTracker mTracker; 

    final int MAX_FACES = 5; 
    final FaceRectangle[] mFacePositions = new FaceRectangle[MAX_FACES]; 
    final long[] mIDs = new long[MAX_FACES]; 
    final Lock faceLock = new ReentrantLock(); 
    int mTouchedIndex; 
    long mTouchedID; 

    int GetFaceFrame(FSDK.FSDK_Features Features, FaceRectangle fr) 
    { 
....... 
     return 0; 
    } 

    public ProcessImageAndDrawResults(Context context) { 
     super(context); 
......... 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     ........... 
     super.onDraw(canvas); 
    } // end onDraw method 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       for (int i=0; i<MAX_FACES; ++i) { 
        if (rects[i] != null && rects[i].x1 <= x && x <= rects[i].x2 && rects[i].y1 <= y && y <= rects[i].y2 + 30) { 
       .................... 
         break; 
        } 
       } 
       Intent profile = new Intent(mContext, ProfileActivity.class); 
       mContext.startActivity(profile); 

     } 
     return true; 
    } 

    static public void decodeYUV420SP(byte[] rgb, byte[] yuv420sp, int width, int height) { 
    ..................... 
    } 
} // end of ProcessImageAndDrawResults class 


// Show video from camera and pass frames to ProcessImageAndDraw class 
class Preview extends SurfaceView implements SurfaceHolder.Callback { 
    Context mContext; 
    Camera mCamera; 

    Preview(Context context, ProcessImageAndDrawResults draw) { 
     super(context); 
     mContext = context; 
     ............... 
    } 

    //SurfaceView callback 
    public void surfaceCreated(SurfaceHolder holder) { 
     ................... 
    } 

    //SurfaceView callback 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     ............... 
    } 
.................. 
} // end of Preview class 

當我補充一點:

Intent profile = new Intent(mContext, ProfileActivity.class); 
mContext.startActivity(profile); 

它給了我以下異常:

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.luxand.facerecognition, PID: 3359 
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.luxand.facerecognition/com.luxand.facerecognition.ProfileActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
         at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:158) 
         at android.app.ActivityThread.main(ActivityThread.java:7229) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
        Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
         at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:311) 
         at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:280) 
         at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:254) 
         at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
         at com.luxand.facerecognition.ProfileActivity.onCreate(ProfileActivity.java:11) 
         at android.app.Activity.performCreate(Activity.java:6876) 
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)  
         at android.app.ActivityThread.access$1100(ActivityThread.java:222)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:158)  
         at android.app.ActivityThread.main(ActivityThread.java:7229)  
         at java.lang.reflect.Method.invoke(Native Method) 

對不起,我添加了以下內容: 我的ProfileActivity代碼是一個空的活動,t他是代碼

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class ProfileActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile); 
    } 
} 

我的清單是:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.luxand.facerecognition"> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature 
     android:name="android.hardware.camera.autofocus" 
     android:required="false" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> --> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen"> 
     <activity 
      android:name=".MainActivity" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".ProfileActivity"></activity> 
    </application> 
</manifest> 

誰能幫助我,好嗎?

+4

這是您應該搜索的錯誤引發:java.lang.IllegalStateException:您需要在此活動中使用Theme.AppCompat主題(或後代)。要閱讀堆棧跟蹤,請查找「由...引發」 。總是 –

+1

也是問題是用ProfileActivity不是你提供的代碼 – Selvin

+0

我增加了ProfileActivity代碼和清單代碼 – gdayler

回答

0

如果當你添加

Intent profile = new Intent(mContext, ProfileActivity.class); 
mContext.startActivity(profile); 

然後存在錯誤,ProfileActivity可能是延長AppCompatActivity和你AndroidManifest.xml需要使用錯誤消息中提到的主題。

You need to use a Theme.AppCompat theme (or descendant) with this activity.

您可能需要查看您的styles.xml文件才能更改此設置。

否則,您不能使用AppCompat,只需擴展常規的Activity類即可,正如您在所示代碼中所做的那樣。

+0

我添加了profileactivity代碼和manifest.xml – gdayler

+0

所以,你可以導入AppCompatActivity,那麼你爲什麼不擴展那個類? –

相關問題