-1

I follow this site respond to do (Custom Video Recording Screen) but do not know why got RuntimeException...

This is error log:

    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>
Community
  • 1
  • 1

2 Answers2

1

You have declared Fragment in Manifest file within in the activity tag

 android:name="com.example.capturevideo.CaptureVideo" // must be removed from manifest

Only Activities are declared in manifest file. Fragments are attached to the activity usually a view group such as FrameLayout which acts as the container.

Declare Activity in manifest. Attach CaptureVideo Fragment to Activity.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @user1621624 you need to have Activity first. You host your Fragment in that Activity. If you don't need fragment then just have the activity. Only Activities are declared in manifest. You should read the docs first – Raghunandan Jul 12 '14 at 05:27
  • @user1621624 Do you know about fragments?? – Raghunandan Jul 13 '14 at 06:02
  • @user1621624, You just need to copy the answer by Rishabh Srivastava in the original StackOverflow link you provided, since in that one he uses an Activity. – Stephan Branczyk Jul 13 '14 at 09:36
0

As the others said: If you do not need to use Fragment, then just stick to Activity. If you can, remove all references and usage of Fragment and replace with Activity.

Simon
  • 28
  • 2