按下按钮后保存到阵列

时间:2014-07-01 15:37:40

标签: android arrays

我试图获取播放嵌入视频时暂停所花费的时间。当我在视频再次开始播放时添加计算(如下所示)时,我收到错误。错误日志似乎表明我的按钮是问题,但它在下面添加代码行之前工作正常。提前致谢

主:

public class ThirdActivity extends Activity {

myVideoView v;

private static final String TAG = "MEDIA";
private TextView tv;


MediaPlayer.OnCompletionListener videoListener = new MediaPlayer.OnCompletionListener()     {

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d("onCompletion", "Total time paused: " + v.getTotalTimeMillis());


        double totalPause = v.getTotalTimeMillis();
        double percent = 23000/(totalPause+23000);
        int pauseNumber = v.pauseCount();
        //long[] pauseTimes = v.pauseDuration();


        File root = android.os.Environment.getExternalStorageDirectory();

        File dir = new File (root.getAbsolutePath() + "/download");
        dir.mkdirs();

        Date e = new Date();
        CharSequence s = DateFormat.format("MM-dd-yy HH-mm-ss", e.getTime());

        File file = new File(dir, s.toString() + "videoStats1.txt");

        String p = "Percent="+String.valueOf(percent)+"\n\nNumber of Pauses="+String.valueOf(pauseNumber);

        String[] r = null;
        /**
        if (pauseNumber > 0) {
            for (int t=0; t<=pauseNumber; t++){
                r[t] = "Pause"+String.valueOf(pauseNumber+1)+" duration="+String.valueOf(pauseTimes[t])+"\n\n";
            }
        }
        **/     
        try {
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println(p);
            /**
            for (int t=0; t<=pauseNumber; t++){
                pw.println(r[t]);
            }
            **/
            pw.flush();
            pw.close();
        } catch (FileNotFoundException f) {
            f.printStackTrace();
            Log.i(TAG, "***** File not found. Did you" + " add a permission to the manifest?");
        }

        Intent intent = new Intent(ThirdActivity.this, FourthActivity.class);
        startActivity(intent);
    }
};

public void PlayPause(View view) {
    //Do something in response to button press
    if(v.isPlaying()){
        v.pause();
    } else {
        v.start();
    }
}

我的视频观看课程:

public class myVideoView extends VideoView{

long lastPausedTime  = 0;      // The time of the last pause (milliseconds)
long totalPausedTime = 0;      // The total time paused (milliseconds)
int numberOfPauses   = 0;      // Number of Pauses
long[] thisPause     = null;   // Array of individual Pause durations

public myVideoView(Context context) {
      super(context);
    }

    public myVideoView(Context context, AttributeSet attrs) {
      super(context, attrs);
    }

    public myVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
    }

@Override
public void pause() {
    lastPausedTime = System.currentTimeMillis();

    super.pause();
}

@Override
public void start() {   
    if (lastPausedTime != 0) {
        long startTime = System.currentTimeMillis();
        totalPausedTime += startTime - lastPausedTime;  
        thisPause[numberOfPauses] = startTime - lastPausedTime; //makes app fail
        numberOfPauses++;
    }       
    super.start();
}

public long getTotalTimeMillis() {
    return totalPausedTime;
}

public int pauseCount() {
    return numberOfPauses;
}

错误日志:

07-01 11:24:05.521: E/AndroidRuntime(25972): FATAL EXCEPTION: main
07-01 11:24:05.521: E/AndroidRuntime(25972): Process: com.example.motorimagery, PID: 25972
07-01 11:24:05.521: E/AndroidRuntime(25972): java.lang.IllegalStateException: Could not execute method of the activity
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.view.View$1.onClick(View.java:3823)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.view.View.performClick(View.java:4438)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.view.View$PerformClick.run(View.java:18422)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.os.Handler.handleCallback(Handler.java:733)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.os.Looper.loop(Looper.java:136)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.app.ActivityThread.main(ActivityThread.java:5017)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at java.lang.reflect.Method.invoke(Method.java:515)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at dalvik.system.NativeStart.main(Native Method)
07-01 11:24:05.521: E/AndroidRuntime(25972): Caused by: java.lang.reflect.InvocationTargetException
07-01 11:24:05.521: E/AndroidRuntime(25972):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at java.lang.reflect.Method.invoke(Method.java:515)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at android.view.View$1.onClick(View.java:3818)
07-01 11:24:05.521: E/AndroidRuntime(25972):    ... 11 more
07-01 11:24:05.521: E/AndroidRuntime(25972): Caused by: java.lang.NullPointerException
07-01 11:24:05.521: E/AndroidRuntime(25972):    at com.example.motorimagery.myVideoView.start(myVideoView.java:37)
07-01 11:24:05.521: E/AndroidRuntime(25972):    at com.example.motorimagery.ThirdActivity.PlayPause(ThirdActivity.java:92)

1 个答案:

答案 0 :(得分:0)

那是因为将thisPause定义为null但从不给它一个新值,因此在行中

**thisPause[numberOfPauses] = startTime - lastPausedTime;

您的应用程序抛出NullPointerException您必须为thisPause

分配“实际”值

编辑:

再看看之后,我认为只保存最后一个暂停值就足够了。或者你有什么理由需要所有这些吗?

或者您可以使用List来保存暂停,因为数组必须具有预定的大小,不得更改。

List<Long> thisPause = new ArrayList<Long>();

public void pause() {
    lastPausedTime = System.currentTimeMillis();
    thisPause.add(lastPausedTime); //Add the last paused time to the end of the List

    super.pause();
}

public void start() {   
    if (lastPausedTime != 0) {
        long startTime = System.currentTimeMillis();
        totalPausedTime += startTime - lastPausedTime;
        //Option 1 Begin: 
        thisPause.set(numberOfPauses,startTime - lastPausedTime);
        //This should work, assuming you never change number of pauses anywhere else
        numberOfPauses++;
        //Option 1 End


        //Option 2 Begin:
        thisPause.add(startTime-lastPausedTime);
        //option 2 End
    }
    //...
}

如果使用第二个选项,要检索最后一个值,请使用

thisPause.get(thisPause.size()-1);

我希望这对你有用