user3349314
user3349314

Reputation: 21

An error appear when execute android app

I try to reproduce some videos in a sequence, this is the code, and the error message. I create the sd card in eclipse with the option Run -> Debug Configurations, is a 2GB sd card. I copied some videos to the sd card. So, I appreciate your help. Thanks.

MovieG.java

package com.example.movieg;

import java.io.File;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle; 
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.MediaController;
import android.widget.VideoView; 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MovieG extends Activity {

public VideoView videoView;
public String ex_name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_movie_g);

    ex_name = getIntent().getExtras().getString("video_name");

    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(new MediaController(this));
    handler.sendEmptyMessage(1);
}

@SuppressLint("HandlerLeak")
Handler handler = new Handler(){

    public void handleMessage(Message msg){

        int pos=msg.what;
        if (pos == 1){

            File ex_name = null;
                videoView.setVideoPath(Environment.getExternalStorageDirectory()+"/"+ex_name+".mp4");
            videoView.requestFocus();
            videoView.start();

            Log.d("Before Video Finish", "i m in before video finish");
            videoView.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    finish();
                }
            });
          }
       }
    };

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.movie_g, menu);
    return true;
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.movieg"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity
        android:name="com.example.movieg.MovieG"
        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>

Error Message

02-24 21:42:54.916: D/AndroidRuntime(278): Shutting down VM
02-24 21:42:54.916: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-24 21:42:54.946: E/AndroidRuntime(278): FATAL EXCEPTION: main
02-24 21:42:54.946: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.movieg/com.example.movieg.MovieG}: java.lang.NullPointerException
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-24 21:42:54.946: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:42:54.946: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
02-24 21:42:54.946: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-24 21:42:54.946: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-24 21:42:54.946: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
02-24 21:42:54.946: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
02-24 21:42:54.946: E/AndroidRuntime(278):  at com.example.movieg.MovieG.onCreate(MovieG.java:28)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-24 21:42:54.946: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-24 21:42:54.946: E/AndroidRuntime(278):  ... 11 more

Upvotes: 0

Views: 83

Answers (2)

Vikas Rathod
Vikas Rathod

Reputation: 384

You have not initialize the videoview:

videoView =(VideoView)findViewById(R.id.your_id);

before 28 th line

Upvotes: 0

Szymon
Szymon

Reputation: 43023

You should verify that getIntent() has extras:

if (getIntent().getExtras() != null)
    ex_name = getIntent().getExtras().getString("video_name");

You then need to cater for ex_name being null later in the code.

Second thing is, you never initialise videoView variable. You need to create an instance of it before it's used in onCreate().

Upvotes: 2

Related Questions