使用多个列表时出现问题

时间:2014-01-29 07:00:54

标签: android

我能够运行我的程序,但是一旦我进入这个活动,应用程序崩溃,我不知道为什么,我的想法,我真的很感激任何帮助!非常感谢你!

    package com.malthorn.zenstatemeditation;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

class Mp3Filter implements FilenameFilter {

    @Override
    public boolean accept(File dir, String filename) {
        return (filename.startsWith("Zen"));
    }

}

public class MeditateScreen extends ListActivity {
    private static final String SD_PATH = new String("/sdcard/");
    private List<String> songs = new ArrayList<String>();
    private MediaPlayer mp = new MediaPlayer();
    String[] times = new String[]{"3 minutes", "5 minutes", "10 minutes", "20 minutes"};
    TextView timerValue = (TextView) findViewById(R.id.timerValue); 

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

        }   

    public void stopMusic(View view) {

        mp.stop();

    }

    public void onItemClick(AdapterView<?> a, View v, int position, long id) {

        if( v.getId() == R.id.songList1 )
        {
            try {

                mp.reset();
                mp.setDataSource(SD_PATH + songs.get(position));
                mp.prepare();
                mp.start();

            } catch (IOException e) {
                Log.v(getString(R.string.app_name), e.getMessage());
            }
        }
        else if( v.getId() == R.id.timeList1 )
        {
            //  String[] times = new String[]{"3 minutes", "5 minutes", "10 minutes", "20 minutes"};
            if(position == 0) {

                new CountDownTimer(180000, 1000) {

                     public void onTick(long millisUntilFinished) {
                         timerValue.setText("time remaining: " + millisUntilFinished / 1000);
                     }

                     public void onFinish() {
                         timerValue.setText("Session finished!");
                     }
                  }.start();

            } else if(position == 1) {

                new CountDownTimer(300000, 1000) {

                 public void onTick(long millisUntilFinished) {
                     timerValue.setText("time remaining: " + millisUntilFinished / 1000);
                 }

                 public void onFinish() {
                     timerValue.setText("Session finished!");
                 }
              }.start();

            } else if(position == 2) {
                new CountDownTimer(600000, 1000) {

                 public void onTick(long millisUntilFinished) {
                     timerValue.setText("time remaining: " + millisUntilFinished / 1000);
                 }

                 public void onFinish() {
                     timerValue.setText("Session finished!");
                 }
              }.start();

            } else if(position == 3) {
                new CountDownTimer(1200000, 1000) {

                 public void onTick(long millisUntilFinished) {
                     timerValue.setText("time remaining: " + millisUntilFinished / 1000);
                 }

                 public void onFinish() {
                     timerValue.setText("Session finished!");
                 }
              }.start();

            }

        }

        }

    public void pauseResume(View view) {
        Button pauseResume = (Button) findViewById(R.id.bPauseResume);

        if(mp.isPlaying()){
            mp.pause();
            pauseResume.setText("Resume");
        } else {
            mp.start();
            pauseResume.setText("Pause");

        }
    }

    public void updatePlaylist() {

        File extStore = Environment.getExternalStorageDirectory();
        File home = new File(extStore.getAbsolutePath() + "/music/");
        if (home.listFiles(new Mp3Filter()).length > 0) {
            for (File file : home.listFiles(new Mp3Filter())) {
                songs.add(file.getName());
            }

            ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
                    R.layout.song_item, songs);
            setListAdapter(songList);
        }

    }
    public void updateTimeList() {


            ArrayAdapter<String> timeList = new ArrayAdapter<String>(this,
                    R.layout.song_item, times);
            setListAdapter(timeList);
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.meditate_screen, menu);
        return true;
    }

}

我的XML格式:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MeditateScreen" >

    <ListView
        android:id="@+id/songList1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bStop"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

    <Button
        android:id="@+id/bStop"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stopMusic"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Stop Music" />

    <Button
        android:id="@+id/bPauseResume"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bStop"
        android:layout_alignBottom="@+id/bStop"
        android:onClick="pauseResume"
        android:layout_alignRight="@id/songList1"
        android:layout_marginRight="47dp"
        android:text="Pause/Resume" />

    <Button
        android:id="@+id/bTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/bStop"
        android:layout_below="@+id/bStop"
        android:layout_marginTop="20dp"
        android:onClick="startTimer"
        android:text="Start Timer" />

    <TextView
        android:id="@+id/timerValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bTimer"
        android:layout_alignBottom="@+id/bTimer"
        android:layout_alignRight="@+id/bPauseResume"
        android:layout_marginRight="22dp"
        android:text="0:00" />

    <ListView
         android:id="@+id/timeList1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/bTimer"
        android:layout_below="@+id/bTimer" >
    </ListView>

</RelativeLayout>

我的LogCat错误:

01-29 01:56:27.626: E/AndroidRuntime(1105): FATAL EXCEPTION: main
01-29 01:56:27.626: E/AndroidRuntime(1105): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.malthorn.zenstatemeditation/com.malthorn.zenstatemeditation.MeditateScreen}: java.lang.NullPointerException
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.os.Looper.loop(Looper.java:137)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invoke(Method.java:525)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at dalvik.system.NativeStart.main(Native Method)
01-29 01:56:27.626: E/AndroidRuntime(1105): Caused by: java.lang.NullPointerException
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.Activity.findViewById(Activity.java:1853)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at com.malthorn.zenstatemeditation.MeditateScreen.<init>(MeditateScreen.java:41)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at java.lang.Class.newInstanceImpl(Native Method)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at java.lang.Class.newInstance(Class.java:1130)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-29 01:56:27.626: E/AndroidRuntime(1105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
01-29 01:56:27.626: E/AndroidRuntime(1105):     ... 11 more

2 个答案:

答案 0 :(得分:0)

setContentView(R.layout.activity_meditate_screen)之后; 您必须定义您在活动中使用的所有视图。

在设定值之前 -

 timerValue.setText(YOUR VALUE);

使用
 TextView = findViewById(R.id.timerValue);

希望这项工作......

答案 1 :(得分:0)

有3点可能导致崩溃

1)如果您使用ListActivity,则需要至少有ListView@android:id/list,否则如果您尝试setListAdapter该应用将会崩溃。

2)如果您的SD卡中没有名为music的文件夹,那么

File extStore = Environment.getExternalStorageDirectory();
File home = new File(extStore.getAbsolutePath() + "/music/");
if (home.listFiles(new Mp3Filter()).length > 0)

将使应用程序崩溃,因为它无法找到文件路径

3)你做不到

TextView timerValue = (TextView) findViewById(R.id.timerValue); 

在致电setContentView之前。在onCreate

之后将其移至setContentView