【问题标题】:Why am I getting a force close? [closed]为什么我要逼近? [关闭]
【发布时间】:2012-12-11 19:38:43
【问题描述】:

我知道我已经问过这个问题,但是在我改变了他告诉我的第一个答案之后没有人回答,但仍然没有结果

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 错误

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

【问题讨论】:

  • 您发送的代码过多,这就是您没有得到答复的原因。阅读日志并过滤您的代码,然后提出问题。我们不能为您做所有事情。
  • 提示:查看 Home.java 中的第 23 行(注意 LogCat 的那部分?),然后尝试找出为什么要将 RelativeLayout 转换为 Button。正如塞尔吉奥所说,我们不能做到这一切。

标签: java android forceclose splash-screen


【解决方案1】:

首先,尝试清理您的项目(例如,从 Eclipse 主菜单中选择 Project > Clean)。如果这没有帮助,你在activity_main_page.xml 中有一个RelativeLayout 显然有错误的android:id 值。它使用的 ID 将基于您的 Home.java 类的第 23 行,无论是哪一行。

【讨论】:

  • 您能否更具体一些,因为 main-page.xml 用于另一个 java 文件。我的 home.java 应该链接到 home.xml 所以我有点困惑???
【解决方案2】:

你不值得但是:

   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,它不是相对布局!

学习阅读,你必须,年轻的帕万!

【讨论】:

  • “你不值得” 如果你不想帮助人们学习,就不要参与。没有必要对此刻薄。
  • 我对这个领域很陌生,我读过它们,但我不明白。我将什么投射到非相对的相对布局。我定义了一个按钮并将其链接到相对布局的 xml 页面
  • 似乎该行是btn_about = (Button) findViewById(R.id.btn_about); 按钮可能在内部使用相对布局,如果您想检查源。您的 XML 布局或视图 ID 可能有问题。
  • 关于您的代码的其他观察:永远不要使用 System.exit(0); 甚至不要在 Android 应用程序上放置退出按钮,如果用户想要存在您的应用程序,他/她将单击主页按钮,如果您想完成您的活动,请致电finish();。您可以通过仅使用一个 OnClickListener 来制作更精简的代码,将其设置为所有按钮并在其中执行 switch(v.getId){ case R.id.btn_start ... }
猜你喜欢
  • 1970-01-01
  • 2020-10-01
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多