-6

I know I have already asked this question but no one answered after the first answer which I changed what he told me and still no result

HOME.java

package com.example.decrypter;

import android.os.Bundle;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.AdapterView.OnItemSelectedListener;

public class Home extends Activity implements OnItemSelectedListener {

Button btn_start,btn_about,btn_exit;
@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

btn_start = (Button) findViewById(R.id.btn_start);
 btn_about = (Button) findViewById(R.id.btn_about);
 btn_exit = (Button) findViewById(R.id.btn_exit);



btn_start.setOnClickListener( new View.OnClickListener() 
{ 



   public void onClick(View v) {
       Intent start = new Intent(Home.this, MainPage.class);
       Home.this.startActivity(start);


}
 });
 btn_about.setOnClickListener( new View.OnClickListener() 
 { 



   public void onClick(View v) {
    // TODO Auto-generated method stub
      Intent about = new Intent(Home.this, about.class);
       Home.this.startActivity(about);


}
  });
 btn_exit.setOnClickListener( new View.OnClickListener() 
 { 



   public void onClick(View v) {
    // TODO Auto-generated method stub
    System.exit(0);

}
 });
 }












 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.home, menu);
    return true;



 }

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

MainPage.java

package com.example.decrypter;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

 public class MainPage extends Activity implements OnItemSelectedListener {
double random1,random2,random3,random4,random5;
int check1,check2,check3,check4,check5;

EditText textbox;
int guess;
String s1,s2,s3,s4,s5;
Spinner spinner1,spinner2,spinner3,spinner4,spinner5;
TextView display1,display2,display3,display4,display5;
Integer[] numbers = {1,2,3,4,5,6,7,8,9};


   @Override
   public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    ArrayAdapter <Integer> adapter1 = new ArrayAdapter <Integer>(this,  
      android.R.layout.simple_spinner_item, numbers);

    setContentView(R.layout.activity_main_page);

    spinner1 =(Spinner) findViewById(R.id.spinner01);
    spinner2= (Spinner) findViewById(R.id.spinner02);
    spinner3= (Spinner) findViewById(R.id.spinner03);
    spinner4= (Spinner) findViewById(R.id.spinner04);
    spinner5= (Spinner) findViewById(R.id.spinner05);


     display1 = (TextView) findViewById(R.id.txtdisplay1);
     display2 = (TextView) findViewById(R.id.txtdisplay2);
     display3 = (TextView) findViewById(R.id.txtdisplay3);
     display4 = (TextView) findViewById(R.id.txtdisplay4);
     display5 = (TextView) findViewById(R.id.txtdisplay5);
    Button btnrandom = (Button) findViewById(R.id.btnrandom);
    Button btn1 = (Button) findViewById(R.id.btn1);






    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(this);
    spinner2.setAdapter(adapter1);
    spinner2.setOnItemSelectedListener(this);
    spinner3.setAdapter(adapter1);
    spinner3.setOnItemSelectedListener(this);
    spinner4.setAdapter(adapter1);
    spinner4.setOnItemSelectedListener(this);
    spinner5.setAdapter(adapter1);
    spinner5.setOnItemSelectedListener(this);





    btnrandom.setOnClickListener( new View.OnClickListener() 
   { 



       public void onClick(View v) {
        // TODO Auto-generated method stub
        random1 = Math.floor(Math.random()*10);
        random2 = Math.floor(Math.random()*10);
        random3 = Math.floor(Math.random()*10);
        random4 = Math.floor(Math.random()*10);
        random5 = Math.floor(Math.random()*10);
        //display.setText("random:" + random1);



        /*check1 = Integer.parseInt(spinner1.getSelectedItem().toString()) ;

        */


    }
   });


    btn1.setOnClickListener( new View.OnClickListener() 
   { 



       public void onClick(View v) {
        // TODO Auto-generated method stub




        if(check1==random1){
            display1.setTextColor(Color.GREEN);
            s1= Integer.toString(check1);
            display1.setText(s1);
        }
        else{
            display1.setTextColor(Color.RED);
            s1= Integer.toString(check1);
            display1.setText(s1);
            }

        if(check2==random2){
            display2.setTextColor(Color.GREEN);
            s2= Integer.toString(check2);
            display2.setText(s2);
            }
        else{
            display2.setTextColor(Color.RED);
            s2= Integer.toString(check2);
            display2.setText(s2);
            }

        if(check3==random3){
            display3.setTextColor(Color.GREEN);
            s3= Integer.toString(check3);
            display3.setText(s3);
            }
        else{
            display3.setTextColor(Color.RED);
            s3= Integer.toString(check3);
            display3.setText(s3);
            }

        if(check4==random4){
            display4.setTextColor(Color.GREEN);
            s4= Integer.toString(check4);
            display4.setText(s4);
            }
        else{
            display4.setTextColor(Color.RED);
            s4= Integer.toString(check4);
            display4.setText(s4);
            }

        if(check5==random5){
            display5.setTextColor(Color.GREEN);
            s5= Integer.toString(check5);
            display5.setText(s5);
            }
        else{
            display5.setTextColor(Color.RED);
            s5= Integer.toString(check5);
            display5.setText(s5);
            }

    }
   });


















}





 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.activity_main_page, menu);
    return true;



}





@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub


    check1 = Integer.parseInt(spinner1.getSelectedItem().toString());


    check2 = Integer.parseInt(spinner2.getSelectedItem().toString()) ;


    check3 = Integer.parseInt(spinner3.getSelectedItem().toString()) ;

    check4 = Integer.parseInt(spinner4.getSelectedItem().toString()) ;


    check5 = Integer.parseInt(spinner5.getSelectedItem().toString()) ;

    }







@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}





}

Splash.java

 package com.example.decrypter;

 import android.app.Activity;
 import android.content.Intent;
 import android.media.MediaPlayer;
 import android.os.Bundle;

      public class Splash extends Activity {
MediaPlayer splashSong;
@Override
protected void onCreate(Bundle splashpage) {
    // TODO Auto-generated method stub
    super.onCreate(splashpage);
    setContentView(R.layout.splash);
    splashSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
    splashSong.start();


    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
            } catch (InterruptedException e){
                e.printStackTrace();
            } finally {
                Intent openHome = new Intent  
                                                  ("com.example.decrypter.HOME");
                startActivity(openHome);
            }
        }
    };
    timer.start();

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    //splashSong.release();
    finish();
}




  }

Android Manifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.decrypter"
 android:versionCode="1"
   android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Home"
        android:label="@string/title_activity_home" >
        <intent-filter>
            <action android:name="com.example.decrypter.HOME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <activity
        android:name=".about"
        android:label="about" >
        <intent-filter>
            <action android:name="com.example.decrypter.ABOUT" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <activity
        android:name=".MainPage"
        android:label="MainPage" >
        <intent-filter>
            <action android:name="com.example.decrypter.MAINPAGE" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
     </activity>
 </application>

 </manifest>

LOGCAT ERROR

12-11 11:36:27.182: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 55K, 53% free 2570K/5379K, external 2131K/2137K, paused 53ms
12-11 11:36:27.271: D/AndroidRuntime(336): Shutting down VM
12-11 11:36:27.271: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-11 11:36:27.291: E/AndroidRuntime(336): FATAL EXCEPTION: main
12-11 11:36:27.291: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.decrypter/com.example.decrypter.Home}: java.lang.ClassCastException: android.widget.RelativeLayout
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.os.Looper.loop(Looper.java:123)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-11 11:36:27.291: E/AndroidRuntime(336):  at java.lang.reflect.Method.invokeNative(Native Method)
12-11 11:36:27.291: E/AndroidRuntime(336):  at java.lang.reflect.Method.invoke(Method.java:507)
12-11 11:36:27.291: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-11 11:36:27.291: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-11 11:36:27.291: E/AndroidRuntime(336):  at dalvik.system.NativeStart.main(Native Method)
12-11 11:36:27.291: E/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
12-11 11:36:27.291: E/AndroidRuntime(336):  at com.example.decrypter.Home.onCreate(Home.java:23)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 11:36:27.291: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-11 11:36:27.291: E/AndroidRuntime(336):  ... 11 more
2
  • 1
    You send too much code, that's why you didn't get an answer. Read the log and filter your code, and then ask a question. We can't do everything for you. Dec 11, 2012 at 19:43
  • Hint: Check out line 23 in Home.java (notice that part of the LogCat?), and then try to figure out why you're casting a RelativeLayout to a Button. As Sergio said, we can't do it all.
    – Cat
    Dec 11, 2012 at 19:44

2 Answers 2

2

First, try cleaning your project (e.g., Project > Clean from the Eclipse main menu). If that does not help, you have a RelativeLayout in activity_main_page.xml that has the wrong android:id value, apparently. Which ID it is using would be based on line 23 of your Home.java class, whichever line that is.

1
  • could you be more specific because main-page.xml is for another java file. my home.java should be linked to home.xml so im kinda confused??? Dec 11, 2012 at 21:34
1

you don't deserve but:

   12-11 11:36:27.291: E/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
   12-11 11:36:27.291: E/AndroidRuntime(336):  at com.example.decrypter.Home.onCreate(Home.java:23)

that means... on com.example.decrypter.Home.OnCreate, on line 23 you're casting something to RelativeLayout, and it's NOT a relative layout!

learn to read, you must, young padwan!

4
  • 3
    "you don't deserve" If you don't want to help people learn, don't participate. There is no need to be snarky about it.
    – Sam
    Dec 11, 2012 at 19:54
  • Im very new to this field and I read them but I do not get it. What am I casting to relative layout that is not relative. Im defining a button and linking it to xml page which is relative layout Dec 11, 2012 at 21:32
  • it seems that the line is btn_about = (Button) findViewById(R.id.btn_about); Button might use internally a relative layout, if you want to check the source. Probably something wrong on your XML layout or the view ID.
    – Budius
    Dec 12, 2012 at 9:21
  • Other observations regarding your code: do never ever ever use System.exit(0); don't even put an exit button on an Android app, if the user wants to exist your app he/she will click the home button, if you want to finish your activity call finish(); on it. You can make a much slimmer code by using only one OnClickListener, setting it to all the buttons and inside it do switch(v.getId){ case R.id.btn_start ... }
    – Budius
    Dec 12, 2012 at 9:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.