我试图制作一个 android 视频播放器来播放 sd 卡中的一些视频文件。为此,我编写了两个类 Main.java 和 VideoSelector.java,但是在启动我的应用程序时它显示了一个空指针异常。请帮我解决这个错误
主.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class Main extends Activity {
private VideoView m_vvPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_vvPlayer = (VideoView) findViewById(R.id.vvMain);
Button btnSelectFile = (Button) findViewById(R.id.btnSelectFile);
btnSelectFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(v.getContext(), VideoSelector.class);
startActivityForResult(i, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode) {
Bundle bd = data.getExtras();
String path = bd.getString(VideoSelector.FILE_PATH);
m_vvPlayer.setVideoPath(path);
m_vvPlayer.start();
}
}
}
视频选择器.java
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class VideoSelector extends ListActivity {
public static final String FILE_PATH = "FILE_PATH";
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
init();
}
public void init() {
setContentView(R.layout.video_selector);
MakeCursor();
// Map Cursor columns to views defined in media_list_item.xml
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
mCursor,
new String[] { MediaStore.Video.Media.TITLE},
new int[] { android.R.id.text1 });
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
String filePath = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA));
mCursor.moveToPosition(position);
Intent result = new Intent();
result.putExtra(FILE_PATH, filePath);
setResult(RESULT_OK, result);
finish();
}
private void MakeCursor() {
String[] cols = new String[] {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.MIME_TYPE,
MediaStore.Video.Media.ARTIST
};
ContentResolver resolver = getContentResolver();
if (resolver == null) {
System.out.println("resolver = null");
} else {
mSortOrder = MediaStore.Video.Media.TITLE + " COLLATE UNICODE";
mWhereClause = MediaStore.Video.Media.TITLE + " != ''";
mCursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
cols, mWhereClause , null, mSortOrder);
}
}
private Cursor mCursor;
private String mWhereClause;
private String mSortOrder;
}
日志猫
08-04 11:28:18.862: I/ApplicationPackageManager(12180): cscCountry is not German : INS
08-04 11:28:18.933: W/dalvikvm(12180): threadid=1: thread exiting with uncaught exception (group=0x40018578)
08-04 11:28:18.933: E/AndroidRuntime(12180): FATAL EXCEPTION: main
08-04 11:28:18.933: E/AndroidRuntime(12180): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Main}: java.lang.NullPointerException
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.os.Looper.loop(Looper.java:130)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-04 11:28:18.933: E/AndroidRuntime(12180): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 11:28:18.933: E/AndroidRuntime(12180): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 11:28:18.933: E/AndroidRuntime(12180): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-04 11:28:18.933: E/AndroidRuntime(12180): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-04 11:28:18.933: E/AndroidRuntime(12180): at dalvik.system.NativeStart.main(Native Method)
08-04 11:28:18.933: E/AndroidRuntime(12180): Caused by: java.lang.NullPointerException
08-04 11:28:18.933: E/AndroidRuntime(12180): at com.example.test.Main.onCreate(Main.java:22)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-04 11:28:18.933: E/AndroidRuntime(12180): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-04 11:28:18.933: E/AndroidRuntime(12180): ... 11 more