2014-10-10 53 views
0

我是Android開發新手。我想開發可以播放歌曲列表的視頻播放器應用程序。我得到如下錯誤 由android.view.View.Configuration.get(ViewConfiguration.java:331) android.view.View ..上的java.lang.NullPointerException引起的。請幫我解決這個問題。android.view中的java.lang.NullPointerException ViewConfiguration.get(ViewConfiguration.java:331)

下面是我的主要代碼

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

你'mv'對象創建的兩倍。所以只需用'VideoView mv;'替換這個'VideoView mv = new VideoView(getBaseContext());'' – Piyush 2014-10-10 12:03:07

回答

0

您實例VideoViewonCreate()前,用

VideoView mv = new VideoView(getBaseContext()); 

改變它與VideoView mv;

+0

你在行號174 – SathishKumar 2014-10-10 12:34:28

+0

「songTitleLabel.setText(songTitle);」在playSong(int songIndex)函數中。 – 2014-10-10 12:38:10

+0

查看'R.id.songTitle'來自'activity_main'佈局 – SathishKumar 2014-10-10 12:46:25

0

其問題更換

VideoView mv = new VideoView(getBaseContext());

VideoView mv; 

(你initilzed在可變的onCreate()方法。)

+0

我已經完成了所需的更改,但現在我在不同的位置(在com.example.mediaplayerdemo.MainActivity.playSong(MainActivity.java:174)我發佈了新的logcat。 at com.example.mediaplayerdemo.MainActivity.onCreate(MainActivity.java:89))。 – 2014-10-10 12:24:17

+0

請評論174行 – Nooh 2014-10-10 12:27:46

相關問題