-1

This was working before, and I made no changes, but now as soon as I open this activity, it crashes.. I tried one list and it worked fine, so I added another list then it started crashing, so I commented out the second list, but it still crashes. Any advice on how to stop the crashing would be greatly appreciated, thanks!

Heres my code:

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

}

Here is the 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

Heres the 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>
user3224105
  • 267
  • 1
  • 2
  • 8
  • 1
    Clean your project and run it . As seen in your code the `listview` is okay you have rightly passed the id of `ListView` so just cleaning your project will solve the issue. –  Jan 30 '14 at 05:18
  • You are casting a Button to a ListView here: com.malthorn.zenstatemeditation.MeditateScreen.onCreate(MeditateScreen.java:45) – Luis Jan 30 '14 at 05:19
  • 1
    y do u use @+id everywhere, in layout_aliXXX u only to do @id – Pulkit Sethi Jan 30 '14 at 05:22
  • 1
    @PulkitSethi why should op use `@id`. check topic id here http://developer.android.com/guide/topics/ui/declaring-layout.html – Raghunandan Jan 30 '14 at 05:22
  • once @+id is done in an xml, then it can be accessed like that for eg android:layout_alignBottom="@+id/bTimer" android:layout_alignRight="@+id/timeList", the id bTimer has already been defined and this should be done android:layout_alignBottom="@id/bTimer" android:layout_alignRight="@id/timeList like this – Pulkit Sethi Jan 30 '14 at 05:25
  • And remove the `@+id when refrencing some other widget`. You should use `@id not @+id` see this http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android –  Jan 30 '14 at 05:25
  • 1
    @PulkitSethi but that is not a problem here. If its already entered in R.java it will skip entering again although using `@id` for referencing later once defined is also not wrong. – Raghunandan Jan 30 '14 at 05:26
  • Where you are calling the `pauseResume()` method ? – GrIsHu Jan 30 '14 at 05:28
  • Are you sure that the xml name is activity_meditate_screen.xml? – Mahdi-bagvand Jan 30 '14 at 05:28
  • Initialize your `List> timesList = new ArrayList>();` inside your `initList()` method. – GrIsHu Jan 30 '14 at 05:30
  • @user3224105 can u please post ur menu xml file – Pulkit Sethi Jan 30 '14 at 05:37
  • cleaning the project and running it again fixed it somehow, thanks a lot @user3110424 If you post it as an answer I can mark it correct for you – user3224105 Jan 30 '14 at 05:39

3 Answers3

5

Clean your project and run it . As seen in your code the listview is okay you have rightly passed the id of ListView so just cleaning your project will solve the issue.

  • Just posted my comment as an answer. –  Jan 30 '14 at 05:42
  • Brother how it is possible everything is right there is problem to initiate button to listview – Ravind Maurya Jan 30 '14 at 05:44
  • @RavindMaurya If you see the xml layout of the OP's question he has rightly passed the id of `Listview` and casting it as a `Listview` so the problem is not that line. As @GrlsHu mentioned it happens sometimes quoting from answer of GrlsHu `The error may be occured because of sometimes changing an id eclipse gives this sought of error as it refers to the wrong id's from R.java.` –  Jan 30 '14 at 05:47
  • this is the id in xml android:id="@+id/songList" and he is using ListView lv = (ListView) findViewById(R.id.timeList); this way > please look at and i have already given answer you can look at once. – Ravind Maurya Jan 30 '14 at 05:50
  • @RavindMaurya Please look the full xml code of him he has two listview one is ` ` –  Jan 30 '14 at 05:52
  • @RavindMaurya I THINK ITS NOW CLEAR TO YOU ? Thanks :-) –  Jan 30 '14 at 05:56
0

I have checked your code its completely working fine you just need to clean and build again and run it.

Reason for error:

The error may be occures because of sometimes changing an id eclipse gives this sought of error as it refers to the wrong id's from R.java. So cleaning project will always work.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Brother how it is possible everything is right there is problem to initiate button to listview – Ravind Maurya Jan 30 '14 at 05:45
  • @RavindMaurya I have checked his code on my side and its working fine there is no issue . – GrIsHu Jan 30 '14 at 05:49
  • this is the id in xml android:id="@+id/songList" and he is using ListView lv = (ListView) findViewById(R.id.timeList); this way > please look at and i have already given answer you can look at once. – Ravind Maurya Jan 30 '14 at 05:51
  • Have you checked his xml layout he has got two listview @RavindMaurya – GrIsHu Jan 30 '14 at 05:53
0

I have rearrange your xml and your code is working fine. I have tested it. Just put your "SongList" list view after creation of id "@id/bPauseResume" and "@id/bStop". New Xml is as :

<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>
Radheshyam Singh
  • 479
  • 3
  • 10