为什么我要关闭一支部队?

问题描述:

我知道我已经问过这个问题,但我改变了第一个答案后无人接听他告诉我,还是没有结果为什么我要关闭一支部队?

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




    } 

的机器人的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的错误

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 
+0

你发送了太多的代码,这就是为什么你没有得到答案。阅读日志并过滤代码,然后提出问题。我们无法为你做所有事情。 –

+0

提示:查看Home.java中的第23行(注意LogCat的一部分?),然后尝试弄清楚为什么要将'RelativeLayout'投射到'Button'。正如塞尔吉奥所说,我们无法做到这一切。 – Eric

首先,请尝试清洁您的项目(例如,项目>从Eclipse主菜单清洁)。如果这没有帮助,显然中的RelativeLayout的值有错误的android:id值。使用哪个ID将根据您的Home.java类的第23行,无论哪一行。

+0

你可能更具体,因为main-page.xml是另一个java文件。我的home.java应该链接到home.xml,所以我有点糊涂? – user1890159

你不值得,但是:

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) 

这意味着......在com.example.decrypter.Home.OnCreate,上线23所强制转换的东西RelativeLayout的,而且相对不布局!

学会阅读,你必须,年轻padwan!

+3

“你不值得”如果你不想帮助人们学习,不要参与。没有必要对此发表任何暗示。 – Sam

+0

我对这个领域非常陌生,我读过它们,但我不明白。我要投射到相对不相对的布局。我定义一个按钮,并将其链接到xml页面,这是相对布局 – user1890159

+0

似乎该行是'btn_about =(Button)findViewById(R.id.btn_about);'按钮可能会在内部使用相对布局,如果你想检查来源。可能你的XML布局或视图ID有问题。 – Budius