ListView崩溃活动没有变化

时间:2014-01-30 05:15:41

标签: android

之前有效,我没有做任何更改,但是现在我打开这个活动就崩溃了..我尝试了一个列表并且工作正常,所以我添加了另一个列表然后它开始崩溃,所以我发表了评论排除第二个列表,但它仍然崩溃。任何关于如何停止崩溃的建议都将非常感谢,谢谢!

继承我的代码:

package com.malthorn.zenstatemeditation;

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

import android.app.Activity;
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.SimpleAdapter;
import android.widget.TextView;


public class MeditateScreen extends Activity {
    private MediaPlayer mp = new MediaPlayer();


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

        //---------------- timer items ----------------

        initList();
        SimpleAdapter simpleAdpt = new SimpleAdapter(this, timesList, android.R.layout.simple_list_item_1, new String[] {"time"}, new int[] {android.R.id.text1});
        ListView lv = (ListView) findViewById(R.id.timeList);
        lv.setAdapter(simpleAdpt);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentAdapter, View view, int position,long id) {

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

                if(position == 0) {
                    new CountDownTimer((300 * 1000), 1000) {

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

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();
                } else if (position == 1) {
                    new CountDownTimer((600 * 1000), 1000) {

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

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();
                } else if (position == 2) {
                    new CountDownTimer((900 * 1000), 1000) {

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

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();
                } else if (position == 3) {
                    new CountDownTimer((1200 * 1000), 1000) {

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

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();
                } else if (position == 4) {
                    new CountDownTimer((1800 * 1000), 1000) {

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

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();
                } else if (position == 5) {
                    new CountDownTimer((2700 * 1000), 1000) {

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

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();
                }


            }
       });

        }   


    List<Map<String, String>> timesList = new ArrayList<Map<String,String>>();


       private void initList() {

        timesList.add(createtime("time", "5 minutes"));
        timesList.add(createtime("time", "10 minutes"));
        timesList.add(createtime("time", "15 minutes"));
        timesList.add(createtime("time", "20 minutes"));
        timesList.add(createtime("time", "30 minutes"));
        timesList.add(createtime("time", "45 minutes"));

    }

    private HashMap<String, String> createtime(String key, String name) {
        HashMap<String, String> time = new HashMap<String, String>();
        time.put(key, name);

        return time;
    } 


    public void stopMusic(View view) {

        mp.stop();

    }



    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");

        }
    }

    @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;
    }

}

这是LogCat

01-30 00:10:23.587: E/AndroidRuntime(11085): FATAL EXCEPTION: main
01-30 00:10:23.587: E/AndroidRuntime(11085): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.malthorn.zenstatemeditation/com.malthorn.zenstatemeditation.MeditateScreen}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ListView
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.os.Looper.loop(Looper.java:137)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.ActivityThread.main(ActivityThread.java:5103)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at java.lang.reflect.Method.invokeNative(Native Method)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at java.lang.reflect.Method.invoke(Method.java:525)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at dalvik.system.NativeStart.main(Native Method)
01-30 00:10:23.587: E/AndroidRuntime(11085): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ListView
01-30 00:10:23.587: E/AndroidRuntime(11085):    at com.malthorn.zenstatemeditation.MeditateScreen.onCreate(MeditateScreen.java:45)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.Activity.performCreate(Activity.java:5133)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 00:10:23.587: E/AndroidRuntime(11085):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-30 00:10:23.587: E/AndroidRuntime(11085):    ... 11 more

继承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/songList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bStop"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/bPauseResume" >

    </ListView>

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

    <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:text="Pause Timer" />

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

    <Button
        android:id="@+id/bPauseResume"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bTimer"
        android:layout_alignRight="@+id/timeList"
        android:layout_marginRight="25dp"
        android:text="Pause Music" />

    <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/timeList"
        android:text="0:00" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:5)

清理项目并运行它。如您的代码所示,listview is okay you have rightly passed the id of ListView只需清理您的项目即可解决问题。

答案 1 :(得分:0)

我已经检查了你的代码,它完全正常工作,你只需要清理并再次构建并运行它。

错误原因:

可能会发生错误,因为有时候更改了id eclipse,因为它引用了来自R.java的错误的id,所以会产生这种错误。所以清洁项目将始终有效。

答案 2 :(得分:0)

我重新排列了你的xml,你的代码运行正常。我测试了它。在创建id“@ id / bPauseResume”和“@ id / bStop”之后,只需输入“SongList”列表视图。新的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" >



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

<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:text="Pause Timer" />

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

<Button
    android:id="@+id/bPauseResume"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/bTimer"
    android:layout_alignRight="@id/timeList"
    android:layout_marginRight="25dp"
    android:text="Pause Music" />

<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/timeList"
    android:text="0:00" />

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