我是Android开发的新手。我想开发可以播放歌曲列表的视频播放器应用程序。我收到如下错误 引起:android.view.ViewConfiguration.get中的java.lang.NullPointerException(ViewConfiguration.java:331) 在android.view.View ..请帮我解决这个问题。
以下是我的主要代码
MainActivity.java
package com.example.mediaplayerdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.example.mediaplayerdemo.R;
import android.net.Uri;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.VideoView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
@SuppressWarnings("unused")
public class MainActivity extends Activity implements OnCompletionListener, OnSeekBarChangeListener {
VideoView mv = new VideoView(getBaseContext());
public TextView songTitleLabel,startTimeField,endTimeField;
private Handler mHandler = new Handler();;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();;
private SeekBar seekbar;
private ImageButton playButton,stopButton;
public static int oneTimeOnly = 0;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
private SongsManager songManager;
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private Utilities utils;
ImageView imgFavorite;
private int currentSongIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reset();
setContentView(R.layout.activity_main);
Button add = (Button) findViewById(R.id.button1);
mv = (VideoView)findViewById(R.id.videoView1);
mv.setOnCompletionListener(this);
startTimeField =(TextView)findViewById(R.id.textView3);
endTimeField =(TextView)findViewById(R.id.textView4);
seekbar = (SeekBar)findViewById(R.id.seekBar1);
playButton = (ImageButton)findViewById(R.id.imageButton1);
stopButton = (ImageButton)findViewById(R.id.imageButton2);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
songTitleLabel = (TextView) findViewById(R.id.songTitle);
songManager = new SongsManager();
utils = new Utilities();
seekbar.setOnSeekBarChangeListener(this); // Important
songsList = songManager.getPlayList();
playButton.setEnabled(true);
stopButton.setEnabled(false);
mv.setEnabled(true);
/*Start Camera*/
imgFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
open();
}
});
/*End Camera*/
playSong(0);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AddingMusicActivity.class);
startActivityForResult(myIntent, 100);
}
});
playButton.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
//reset();
if(mv.isPlaying()){
if(mv!=null){
VideoView mVideoView = (VideoView)findViewById(R.id.videoView1);
mVideoView.pause();
//mp.pause();
finalTime = mVideoView.getDuration();
startTime = mVideoView.getCurrentPosition();
playButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
else{
if(mv!=null){
finalTime = mv.getDuration();
startTime = mv.getCurrentPosition();
//VideoView mVideoView = (VideoView)findViewById(R.id.videoView1);
//String uriPath1 = "/mnt/sdcard/songsList";
//Uri uri1 = Uri.parse(uriPath1);
//mVideoView.setVideoPath(uriPath1);
//mVideoView.setVideoURI(uri1);
//mVideoView.requestFocus();
//mVideoView.start();
//mp.start();
// Changing button image to pause button
playButton.setEnabled(false);
stopButton.setEnabled(true);
}
}
seekbar.setClickable(true);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(mUpdateTimeTask,100);
}
});
stopButton.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
VideoView mVideoView = (VideoView)findViewById(R.id.videoView1);
mVideoView.stopPlayback();
playButton.setEnabled(true);
mVideoView.setEnabled(false);
}});
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
currentSongIndex = data.getExtras().getInt("songIndex");
}
}
private void playSong(int songIndex) {
try {
mv.refreshDrawableState();
mv.setVideoPath(songsList.get(songIndex).get("songPath"));
mv.setOnPreparedListener(null);
mv.start();
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);
playButton.setImageResource(R.drawable.stop);
seekbar.setProgress(0);
seekbar.setMax(100);
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mv.getDuration();
long currentDuration = mv.getCurrentPosition();
startTimeField.setText(""+utils.milliSecondsToTimer(totalDuration));
endTimeField.setText(""+utils.milliSecondsToTimer(currentDuration));
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
seekbar.setProgress(progress);
mHandler.postDelayed(this, 100);
}
};
private void reset() {
// TODO Auto-generated method stub
}
public void open(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
}
@Override
public void onDestroy(){
super.onDestroy();
mv.suspend();
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
我的LogCat详细信息:
10-10 16:53:40.490: D/AndroidRuntime(8225): Shutting down VM
10-10 16:53:40.490: W/dalvikvm(8225): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
10-10 16:53:40.510: E/AndroidRuntime(8225): FATAL EXCEPTION: main
10-10 16:53:40.510: E/AndroidRuntime(8225): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mediaplayerdemo/com.example.mediaplayerdemo.MainActivity}: java.lang.NullPointerException
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.os.Looper.loop(Looper.java:137)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-10 16:53:40.510: E/AndroidRuntime(8225): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 16:53:40.510: E/AndroidRuntime(8225): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 16:53:40.510: E/AndroidRuntime(8225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-10 16:53:40.510: E/AndroidRuntime(8225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-10 16:53:40.510: E/AndroidRuntime(8225): at dalvik.system.NativeStart.main(Native Method)
10-10 16:53:40.510: E/AndroidRuntime(8225): Caused by: java.lang.NullPointerException
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.view.ViewConfiguration.get(ViewConfiguration.java:331)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.view.View.<init>(View.java:2698)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.view.SurfaceView.<init>(SurfaceView.java:176)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.widget.VideoView.<init>(VideoView.java:91)
10-10 16:53:40.510: E/AndroidRuntime(8225): at com.example.mediaplayerdemo.MainActivity.<init>(MainActivity.java:39)
10-10 16:53:40.510: E/AndroidRuntime(8225): at java.lang.Class.newInstanceImpl(Native Method)
10-10 16:53:40.510: E/AndroidRuntime(8225): at java.lang.Class.newInstance(Class.java:1319)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
10-10 16:53:40.510: E/AndroidRuntime(8225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
10-10 16:53:40.510: E/AndroidRuntime(8225): ... 11 more
10-10 16:53:40.840: I/dalvikvm(8225): threadid=3: reacting to signal 3
10-10 16:53:40.860: I/dalvikvm(8225): Wrote stack traces to '/data/anr/traces.txt'
10-10 16:53:41.111: I/dalvikvm(8225): threadid=3: reacting to signal 3
10-10 16:53:41.130: I/dalvikvm(8225): Wrote stack traces to '/data/anr/traces.txt'
10-10 16:58:40.581: I/Process(8225): Sending signal. PID: 8225 SIG: 9
请在这里帮助我。
答案 0 :(得分:0)
您在VideoView
之前实例化onCreate()
,替换
VideoView mv = new VideoView(getBaseContext());
的 VideoView mv;
答案 1 :(得分:0)
的问题
VideoView mv = new VideoView(getBaseContext());
将其更改为。
VideoView mv;
(你在onCreate()方法中初始化了该变量。)