错误:FATAL EXCEPTION:main无法启动活动ComponentInfo

时间:2017-01-24 01:19:48

标签: android exception ontouchevent

我在mainactivity中有以下代码:

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代码是一个空活动,这是代码

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>

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果添加

时存在错误
Intent profile = new Intent(mContext, ProfileActivity.class );
mContext.startActivity(profile);

然后,ProfileActivity可能正在扩展AppCompatActivity,而您的AndroidManifest.xml需要使用错误消息中提到的主题。

  

您需要在此活动中使用Theme.AppCompat主题(或后代)。

您可能需要查看styles.xml文件才能更改此内容。

否则,您无法使用AppCompat,只需像在显示的代码中那样扩展常规Activity类。