我正在开发一个Android项目,应用程序应调用视频录制界面,允许用户录制视频,然后返回活动处理视频。 我为此目的使用了意图,代码没有任何错误并且工作正常,直到我完成录制视频并按“确定”返回主活动或取消取消录制。当用户按下确定或取消并且在焦点返回到应用程序之前它停止使用消息“不幸的是,appName已停止工作” 我努力在我的代码中找到错误,但我不能 我认为这是“onActvityResult”方法。因为当执行到达它时,应用程序停止。 作为一个初学者,任何人都可以请帮助 非常感谢 这是我的代码,直到现在
package com.example.myproject.vlc;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
//import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);}
//----------------------------------------------------------
//Button btnReadyToGo= (Button)findViewById(R.id.btnReady);
public void onButtonClick(View view1){ //if button is clicked
//TextView label3=(TextView)findViewById(R.id.lbl3);
//label3.setTextColor(Color.CYAN); //change color
//----------------------------invoke the video recording built in interface-----------------------------------------------------------
String CAPTURE_TITLE="MyVideo.3gp";
File myfile = new File(Environment.getExternalStorageDirectory() + "/DCIM/", CAPTURE_TITLE);
Uri outputFileUri = Uri.fromFile( myfile );
Intent intent2 = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
intent2.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent2, REQUEST_VIDEO_CAPTURED);
//----------------------------------------------
//----------------------------------------------
} //end of OnButtonclick funciton
//----------------------------------------------------------------
// private static final int VIDEO_CAPTURE = 101;
final static int REQUEST_VIDEO_CAPTURED = 1;
// Intent intent1 = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// startActivityForResult(intent1, VIDEO_CAPTURE);
//-----------------------------------------------------------------
//---------------------------------after recording finishes--------------------------------
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri2=data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURED) {
if (resultCode == RESULT_OK) {
// Toast.makeText(this, "Video saved to:\n" + uri2, Toast.LENGTH_LONG).show();
Toast.makeText(this, uri2.getPath() , Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Video recording cancelled.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Failed to record video", Toast.LENGTH_LONG).show();
}
}
//------------------------getting video length-------------------------------------
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(Environment.getExternalStorageDirectory() + "/DCIM/"+ "MyVideo.3gp");
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong( time );
long duration = timeInmillisec / 1000;
//-------------------------------------------------------------
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的logcat:
11-20 06:17:30.094 1600-1600/? D/dalvikvm﹕ Late-enabling CheckJNI
11-20 06:17:30.158 1600-1600/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
11-20 06:17:30.162 1600-1600/? I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
11-20 06:17:30.162 1600-1600/? W/dalvikvm﹕ VFY: unable to resolve interface method 14055: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
11-20 06:17:30.162 1600-1600/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
11-20 06:17:30.162 1600-1600/? I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
11-20 06:17:30.162 1600-1600/? W/dalvikvm﹕ VFY: unable to resolve interface method 14059: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-20 06:17:30.162 1600-1600/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
11-20 06:17:30.174 1600-1600/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-20 06:17:30.174 1600-1600/? W/dalvikvm﹕ VFY: unable to resolve virtual method 401: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-20 06:17:30.174 1600-1600/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-20 06:17:30.174 1600-1600/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-20 06:17:30.174 1600-1600/? W/dalvikvm﹕ VFY: unable to resolve virtual method 423: Landroid/content/res/TypedArray;.getType (I)I
11-20 06:17:30.174 1600-1600/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-20 06:17:30.690 1600-1600/com.example.israajaradat.vlc D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
11-20 06:17:30.694 1600-1600/com.example.israajaradat.vlc D/﹕ HostConnection::get() New Host Connection established 0xb8caf700, tid 1600
11-20 06:17:30.714 1600-1600/com.example.israajaradat.vlc D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
11-20 06:17:30.714 1600-1600/com.example.israajaradat.vlc D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
11-20 06:17:30.894 1600-1600/com.example.israajaradat.vlc W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
11-20 06:17:30.898 1600-1600/com.example.israajaradat.vlc E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
11-20 06:17:30.898 1600-1600/com.example.israajaradat.vlc E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192
11-20 06:17:30.974 1600-1600/com.example.israajaradat.vlc E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
11-20 06:17:30.974 1600-1600/com.example.israajaradat.vlc E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192
11-20 06:17:30.974 1600-1600/com.example.israajaradat.vlc D/OpenGLRenderer﹕ Enabling debug mode 0
11-20 06:17:53.886 1600-1600/com.example.israajaradat.vlc D/AndroidRuntime﹕ Shutting down VM
11-20 06:17:53.886 1600-1600/com.example.israajaradat.vlc W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cfab20)
11-20 06:17:53.886 1600-1600/com.example.israajaradat.vlc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.israajaradat.vlc, PID: 1600
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.example.israajaradat.vlc/com.example.israajaradat.vlc.Activity1}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.israajaradat.vlc.Activity1.onActivityResult(Activity1.java:62)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
11-20 06:17:53.894 1600-1600/com.example.israajaradat.vlc D/dalvikvm﹕ GC_FOR_ALLOC freed 241K, 11% free 3777K/4208K, paused 3ms, total 4ms
11-20 06:17:55.690 1600-1600/com.example.israajaradat.vlc I/Process﹕ Sending signal. PID: 1600 SIG: 9