设置视频路径NullPointerException

时间:2014-05-05 08:20:38

标签: android nullpointerexception

filepath为空?为什么?有人能解决我的问题吗?请~3Q

FragmentFile.class向PlayerActivity发送意图

public class FragmentFile extends FragmentBase implements OnItemClickListener {

private FileAdapter mAdapter;
private final String TAG = "FragmentFile";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = super.onCreateView(inflater, container, savedInstanceState);
    mAdapter = new FileAdapter(getActivity(), null);
    mListView.setAdapter(mAdapter);
    mListView.setOnItemClickListener(this);
    new ScanVideoTask().execute();
    return v;
}

/** play */
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final File f = mAdapter.getItem(position);
    Intent intent = new Intent(getActivity(), PlayerActivity.class);
    Bundle bundle = new Bundle(); 
    bundle.putString("path", f.getPath());
    Log.i(TAG, f.getPath());
    intent.putExtras(bundle);
    startActivity(intent);
}

PlayerActivity接收意图

public class PlayerActivity extends Activity {

private final String TAG = "PlayActivity";
private ImageButton btn_playPause, btn_previous, btn_slow, btn_fast, btn_next;
private VideoView VideoView;
private String filepath="";

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

    init();
}

private void init() {
    // TODO Auto-generated method stub
    btn_playPause = (ImageButton) findViewById(R.id.btn_playPause);
    btn_slow= (ImageButton) findViewById(R.id.btn_slow);
    btn_fast= (ImageButton) findViewById(R.id.btn_fast);
    btn_previous= (ImageButton) findViewById(R.id.btn_previous);
    btn_next= (ImageButton) findViewById(R.id.btn_next);

    Bundle bundle = this.getIntent().getExtras();
    filepath = bundle.getString("path");    
    Log.i(TAG,filepath.toString());

    if (filepath.equalsIgnoreCase("")) {
        // Tell the user to provide a media file URL/path.
        Toast.makeText(PlayerActivity.this, "影片存取錯誤", Toast.LENGTH_LONG).show();
        return;
    }else{  

        Toast.makeText(PlayerActivity.this, filepath , Toast.LENGTH_LONG).show();
        Toast.makeText(PlayerActivity.this, "影片準備播放", Toast.LENGTH_LONG).show();
        Log.i(TAG, filepath);
        VideoView.setVideoPath(filepath);


        VideoView.requestFocus();  
        VideoView.start();

        VideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                // optional need Vitamio 4.0
                mediaPlayer.setPlaybackSpeed(1.0f);
            }
        });
    }

logcat的

05-05 18:27:28.991: I/Vitamio[Player](30116): FragmentFile
05-05 18:27:29.271: W/ResourceType(30116): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
05-05 18:27:29.296: D/dalvikvm(30116): GC_CONCURRENT freed 356K, 30% free 9946K/14151K, paused 16ms+7ms, total 45ms
05-05 18:27:29.301: I/Vitamio[Player](30116): PlayActivity
05-05 18:27:29.321: I/Vitamio[Player](30116): PlayActivity
05-05 18:27:29.321: D/AndroidRuntime(30116): Shutting down VM
05-05 18:27:29.321: W/dalvikvm(30116): threadid=1: thread exiting with uncaught exception (group=0x418872a0)
05-05 18:27:29.331: E/AndroidRuntime(30116): FATAL EXCEPTION: main
05-05 18:27:29.331: E/AndroidRuntime(30116): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vitamio_oases/com.example.vitamio_oases.ui.PlayerActivity}: java.lang.NullPointerException
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.os.Looper.loop(Looper.java:137)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.ActivityThread.main(ActivityThread.java:4921)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at java.lang.reflect.Method.invokeNative(Native Method)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at java.lang.reflect.Method.invoke(Method.java:511)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at dalvik.system.NativeStart.main(Native Method)
05-05 18:27:29.331: E/AndroidRuntime(30116): Caused by: java.lang.NullPointerException
05-05 18:27:29.331: E/AndroidRuntime(30116):    at com.example.vitamio_oases.ui.PlayerActivity.init(PlayerActivity.java:58)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at com.example.vitamio_oases.ui.PlayerActivity.onCreate(PlayerActivity.java:34)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.Activity.performCreate(Activity.java:5206)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-05 18:27:29.331: E/AndroidRuntime(30116):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
05-05 18:27:29.331: E/AndroidRuntime(30116):    ... 11 more

4 个答案:

答案 0 :(得分:1)

初始化Intent对象:

Intent intent = new Intent(getActivity(), PlayerActivity.class);
    intent.putExtras("path", f.getPath());
    startActivity(intent);

获取文件路径:

 filepath = getIntent().getExtras().getString("path");
init()内的onCreate()之前

答案 1 :(得分:0)

您还可以上传创建Intent对象的代码吗?

看起来你得到了一个异常,因为filepath的值为null(不是空字符串“”)..

确保在创建Intent时,它具有使用键“path”设置的Extra String值。

答案 2 :(得分:0)

首先,检查活动是否在清单文件中注册。如果您已注册,请使用以下代码检查路径是否为空

if(TextUtils.isEmpty(filepath ))
{

}
else
{

}

答案 3 :(得分:0)

首先必须检查密钥"path"是否与您发送密钥的密钥相同。

也不要使用==运算符比较字符串。为此,您需要使用equalsequalsIgnoreCase

 if (filepath.equalsIgnoreCase("")) {
    // Tell the user to provide a media file URL/path.
    Toast.makeText(PlayerActivity.this, "影片存取錯誤", Toast.LENGTH_LONG).show();
    return;
}

还必须确保找到VideoView的ID。