在活動之間使用意圖錯誤 (using intents between activities error)


問題描述

在活動之間使用意圖錯誤 (using intents between activities error)

使用我得到的 logcat (who=null) 中的意圖從視頻捕獲活動返回時出現運行時錯誤,並且應用程序在該點崩潰(從視頻捕獲活動返回到主要活動時。我的問題是當我在 Genymotion 模擬器上運行應用程序時不會發生這種情況,但是當我在手機上安裝它時,應用程序停止並顯示消息(不幸的是應用程序已停止工作)我試圖通過使用答案來解決我的代碼中的問題以下鏈接

onActivityResult 中的數據為空

通過添加語句:

    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)

參考解法

方法 1:

Did you try using The format Intent intent = new Intent(mainactivity.this, new activity.class); Startactivity(intent);

方法 2:

You can try this...

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);
}

(by hj Eaghil mohammedSRB Bans)

參考文件

  1. using intents between activities error (CC BY‑SA 2.5/3.0/4.0)

#runtime #Android #android-intent






相關問題

通過java運行的執行程序似乎超時? (Executed programs run through java seem to time out?)

Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)

Hentikan proses anak ketika proses induk berhenti (Stop child process when parent process stops)

Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console-script)

選擇不包含主要類型 - 錯誤 (Selection does not contain main type - error)

在活動之間使用意圖錯誤 (using intents between activities error)

在運行時使用 Spring 在表中創建新字段 (Create new field into a table with Spring at runtime)

如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)

我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)

如何計算此代碼的時間複雜度? (How can I compute the time complexity of this code?)

運行 .exe 文件生成 bt jar2exe 軟件時出現 java runtime environment not found 錯誤 (Getting java runtime enviroment not found error while running .exe file produced bt jar2exe software)

在運行時在 VBA 中顯示所有變量及其值 (Show all variables and their values in VBA during runtime)







留言討論