我该如何修复“无法实例化活动”

时间:2014-07-11 08:56:49

标签: android exception android-activity fragment instantiation

我按照这个网站回复做 (Custom Video Recording Screen) 但不知道为什么得到RuntimeException ...

这是错误日志:

    07-11 16:31:33.246: D/ActivityThread(10990): handleBindApplication:com.example.capturevideo
    07-11 16:31:33.246: D/ActivityThread(10990): setTargetHeapUtilization:0.75
    07-11 16:31:33.246: D/ActivityThread(10990): setTargetHeapMinFree:2097152
    07-11 16:31:33.416: D/AndroidRuntime(10990): Shutting down VM
    07-11 16:31:33.416: W/dalvikvm(10990): threadid=1: thread exiting with uncaught exception (group=0x4188cce0)
    07-11 16:31:33.426: E/AndroidRuntime(10990): FATAL EXCEPTION: main
    07-11 16:31:33.426: E/AndroidRuntime(10990): Process: com.example.capturevideo, PID: 10990
    07-11 16:31:33.426: E/AndroidRuntime(10990): java.lang.RuntimeException: Unable to instantiate activity         
    ComponentInfo{com.example.capturevideo/com.example.capturevideo.CaptureVideo}:         java.lang.ClassCastException: com.example.capturevideo.CaptureVideo cannot be cast to android.app.Activity
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.os.Handler.dispatchMessage(Handler.java:102)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.os.Looper.loop(Looper.java:136)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.app.ActivityThread.main(ActivityThread.java:5147)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at java.lang.reflect.Method.invoke(Method.java:515)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at dalvik.system.NativeStart.main(Native Method)
    07-11 16:31:33.426: E/AndroidRuntime(10990): Caused by: java.lang.ClassCastException: com.example.capturevideo.CaptureVideo cannot be cast to android.app.Activity
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2135)
    07-11 16:31:33.426: E/AndroidRuntime(10990):    ... 11 more

CaptureVideo.java

    package com.example.capturevideo;

    import java.io.IOException;
    import java.util.Random;
    import android.app.Activity;
    import android.app.Fragment;
    import android.annotation.SuppressLint;
    import android.content.res.Configuration;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;

    public class CaptureVideo extends Fragment implements OnClickListener, SurfaceHolder.Callback{

private Button btnStartRec;
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
private int randomNum;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view001 = inflater.inflate(R.layout.capture_video,container,false);
    recorder = new MediaRecorder();
    initRecorder();        
    btnStartRec = (Button) view001.findViewById(R.id.btnCaptureVideo);
    btnStartRec.setOnClickListener(this);
    SurfaceView cameraView = (SurfaceView)view001.findViewById(R.id.surfaceCamera);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    cameraView.setClickable(true);
    cameraView.setOnClickListener(this);        

    return view001;
}

 public void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);

 } 

 public void onActivityCreated(Bundle savedInstanceState) 
 {      
     super.onActivityCreated(savedInstanceState);

 }  



    @SuppressLint({ "SdCardPath", "NewApi" })
private void initRecorder() {

    Random rn = new Random();
    int maximum = 10000000;
    int minimum = 00000001;
    int range = maximum  - minimum  + 1;
    randomNum =  rn.nextInt(range) + minimum + 1 - 10;        

    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        recorder.setOrientationHint(90);//plays the video correctly
    }else{
        recorder.setOrientationHint(180);
    }


    recorder.setOutputFile("/sdcard/MediaAppVideos/"+randomNum+".mp4");

}

private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        //finish();
    } catch (IOException e) {
        e.printStackTrace();
        //finish();
    }
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnCaptureVideo:          
            try{
                if (recording) {
                    recorder.stop();
                    recording = false;
                    // Let's initRecorder so we can record again
                    //initRecorder();
                    //prepareRecorder();
                } else {
                    recording = true;
                    recorder.start();                   
                }

            }catch(Exception e){

            }

    default:
        break;

    }
}

public void surfaceCreated(SurfaceHolder holder) {
    prepareRecorder();
}


public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

public void surfaceDestroyed(SurfaceHolder holder) {
    try {
        if (recording) {
            recorder.stop();
            recording = false;
        }
        recorder.release();
        // finish();
    } catch (Exception e) {

    }

}


    }

capture_video.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<SurfaceView
android:id="@+id/surfaceCamera"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<Button
    android:id="@+id/btnCaptureVideo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Start Recording" />

     </RelativeLayout>

的AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.capturevideo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="20" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.capturevideo.CaptureVideo"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>
    </manifest>

2 个答案:

答案 0 :(得分:1)

您已在活动代码

中的Manifest文件中声明了Fragment
 android:name="com.example.capturevideo.CaptureVideo" // must be removed from manifest

仅在清单文件中声明活动。片段通常附加到活动视图组,例如FrameLayout,它充当容器。

在清单中声明活动。将CaptureVideo片段附加到活动。

答案 1 :(得分:0)

正如其他人所说:如果你不需要使用Fragment,那么只需坚持使用Activity。如果可以,请删除Fragment的所有引用和用法,并替换为Activity。