在活动之间使用意图错误

时间:2015-11-21 12:12:11

标签: android android-intent runtime

使用意图从视频捕获活动返回时出现运行时错误 在我得到的logcat中(who = null),应用程序在该点崩溃(从视频捕获活动返回到主活动时)。 我的问题是,当我在Genymotion模拟器上运行应用程序时,这并不好 但当我在手机上安装它时,应用程序会停止显示该消息(不幸的是应用程序已停止工作) 我尝试使用以下链接中的答案解决我的代码中的问题

data in onActivityResult is null

添加声明:

    setResult(RESULT_OK, intent2); //addition

它像我前面提到的那样工作,但只有当应用程序在模拟器而不是手机上运行时

这是我的代码:

package com.example.israajaradat.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="MyVideo89.3gp";
            // Specified the desired location here
            File myfile = new File(Environment.getExternalStorageDirectory() + "/DCIM/", CAPTURE_TITLE);

            Uri outputFileUri = Uri.fromFile( myfile );
            Intent intent2 = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
            setResult(RESULT_OK, intent2); //addition

            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 (data == null) return;
        if(resultCode==RESULT_OK && requestCode==1){
       // 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/"+ "MyVideo89.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-21 07:38:57.317    1737-1737/com.example.israajaradat.vlc E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.israajaradat.vlc, PID: 1737
    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:65)
            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)

2 个答案:

答案 0 :(得分:0)

您是否尝试使用该格式? Intent intent = new Intent(mainactivity.this,new activity.class); Startactivity(意向);

答案 1 :(得分:0)

你可以试试这个......

private String videoPath = "";

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK)
    {

      Uri vid = data.getData();
      videoPath = getRealPathFromURI(vid);
    } 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();
    }


}

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}