java - 执行android应用程序时出现错误

标签 java android eclipse

我尝试按顺序重现一些视频,这是代码和错误消息。我在 eclipse 中使用选项“运行”->“调试配置”创建 sd 卡,是一张 2GB 的 sd 卡。我把一些视频复制到了SD卡上。所以,我很感谢你的帮助。谢谢。

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

最佳答案

您应该验证 getIntent() 是否有额外功能:

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

然后,您需要在代码中稍后将 ex_name 设为 null。

第二件事是,您永远不会初始化 videoView 变量。您需要先创建它的实例,然后才能在 onCreate() 中使用它。

关于java - 执行android应用程序时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22003693/

相关文章:

java - 在mac终端上编译并运行java程序

java - 当我启动服务器时,Mongo 没有更新不在数据库中的文档

java - WakeLock 会导致手机重启吗?

java - 我的自定义 JDialog 出现,但为空

android - 编辑文本 : Toggle InputType. TYPE_TEXT_FLAG_NO_SUGGESTIONS

android - 双面 ViewRenderable

android - 无效的可绘制标签动画矢量

android - 在客户端和服务器端之间共享代码

Eclipse错误: 'Failed to create the Java Virtual Machine'

java - 运行 jar 文件中的 perl 脚本文件