2014-02-25 67 views
0

我嘗試重現序列中的某些視頻,這是代碼和錯誤消息。我使用Run - > Debug Configurations選項在eclipse中創建SD卡,是一個2GB的SD卡。我複製了一些視頻到SD卡。所以,我感謝你的幫助。謝謝。執行android應用程序時出現錯誤

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> 

錯誤消息

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 

回答

2

你應該v erify是getIntent()有演員:

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

然後,您需要迎合ex_name被空後面的代碼。

第二件事,你永遠不會初始化videoView變量。在onCreate()中使用它之前,您需要創建它的一個實例。

+3

'videoView'實例也是空 –

+0

@ρяσѕρєяK這是正確的,謝謝!答案已更新。 – Szymon

0

您還沒有初始化videoview:

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

前28個線路的

相關問題