android中运行时异常

发布于 2024-11-24 17:52:26 字数 7837 浏览 0 评论 0原文

package com.androidbook.triviaquiz;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.Animation.AnimationListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class QuizSplashActivity extends TriviaQuizActivity{
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            Log.e("My App","Error Occured in on create1");
            startAnimating();
            Log.e("My App","Error Occured in on create1");

        }
     public void startAnimating() {
         try{
            TextView logo1 = (TextView) findViewById(R.id.textView1);
            Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
            logo1.startAnimation(fade1);

            Log.e("My App","Error Occured in start animating1");
            TextView logo2 = (TextView) findViewById(R.id.textView2);
            Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
            logo2.startAnimation(fade2);
            Log.e("My App","Error Occured in start animating2");
            fade2.setAnimationListener(new AnimationListener() {
         public void onAnimationEnd(Animation animation) {
                    // The animation has ended, transition to the Main Menu screen
                    startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
                    QuizSplashActivity.this.finish();
         }
         public void onAnimationRepeat(Animation animation) {
         }
         public void onAnimationStart(Animation animation) {
         }
            });
            Log.e("My App","Error Occured in start animating general");
            Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
            LayoutAnimationController controller =
            new LayoutAnimationController(spinin);
            TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
            for (int i = 0; i < table.getChildCount(); i++) {
            TableRow row = (TableRow) table.getChildAt(i);
            row.setLayoutAnimation(controller);
            Log.e("My App","Error Occured in start animating general end ");
            }
         }
         catch(Exception ex)
         {
             Log.e("Exception logged",ex.getStackTrace().toString());

         }
     }

     @Override
     public void onPause() {
         super.onPause();
         TextView logo1 = (TextView) findViewById(R.id.textView1);
         logo1.clearAnimation();

            Log.e("My App","Error Occured in onPause1");

         TextView logo2 = (TextView) findViewById(R.id.textView2);
         logo2.clearAnimation();

            Log.e("My App","Error Occured in onPause1");
         TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
         for (int i = 0; i < table.getChildCount(); i++) {
         TableRow row = (TableRow) table.getChildAt(i);
         row.clearAnimation();
         Log.e("My App","Error Occured in onPause3");
         }
     }
     @Override 
     public void onResume(){
         super.onResume();
         startAnimating();
     }

}

这是清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidbook.triviaquiz"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/quizicon" android:label="@string/app_name" android:debuggable="true">
      <activity android:name=".QuizSplashActivity" android:label="@string/splash">
           <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="QuizGameActivty" android:label="@string/game"></activity>
        <activity android:name="QuizHelpActivity" android:label="@string/help"></activity>
        <activity android:name="QuizMenuActivity" android:label="@string/menu"></activity>
        <activity android:name="QuizScoresActivity" android:label="@string/scores"></activity>
        <activity android:name="QuizsettingsActivity" android:label="@string/settings"></activity>

    </application>

</manifest>

在调试模式下抛出的异常:

{com.androidbook.triviaquiz/com.androidbook.triviaquiz.QuizSplashActivity}: java.lang.ClassCastException: android.widget.TextView

布局文件启动活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="vertical" android:background="@android:color/black" android:layout_weight="0.94" android:weightSum="1">
        <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="0.08" android:text="@string/header" android:textColor="@color/logo_color" android:textSize="24pt" android:id="@+id/textView1"></TextView>
        <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1" android:layout_weight="0.48" android:weightSum="1" android:stretchColumns="*">
            <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_width="fill_parent">
                <ImageView android:src="@drawable/splash1" android:id="@+id/imageView1" android:layout_height="wrap_content" android:layout_width="fill_parent"></ImageView>
                <ImageView android:src="@drawable/splash2" android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
            </TableRow>
            <TableRow android:id="@+id/tableRow2" android:layout_height="wrap_content" android:layout_width="fill_parent">
                <ImageView android:src="@drawable/splash3" android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
                <ImageView android:src="@drawable/splash4" android:id="@+id/imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
            </TableRow>
            <TextView android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="fill_parent" android:textColor="@color/logo_color" android:fadeScrollbars="true" android:focusableInTouchMode="true" android:textSize="24pt" android:text="@string/footer" android:layout_weight="0.36"></TextView>
            </TableLayout>
        <TextView android:layout_width="fill_parent" android:text="@string/version" android:background="@color/version_bkgrd" android:textColor="@color/version_color" android:id="@+id/textView3" android:layout_gravity="center_horizontal" android:layout_height="32dp" android:lineSpacingExtra="@dimen/version_spacing"></TextView>
    </LinearLayout>
</LinearLayout>
package com.androidbook.triviaquiz;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.Animation.AnimationListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class QuizSplashActivity extends TriviaQuizActivity{
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            Log.e("My App","Error Occured in on create1");
            startAnimating();
            Log.e("My App","Error Occured in on create1");

        }
     public void startAnimating() {
         try{
            TextView logo1 = (TextView) findViewById(R.id.textView1);
            Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
            logo1.startAnimation(fade1);

            Log.e("My App","Error Occured in start animating1");
            TextView logo2 = (TextView) findViewById(R.id.textView2);
            Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
            logo2.startAnimation(fade2);
            Log.e("My App","Error Occured in start animating2");
            fade2.setAnimationListener(new AnimationListener() {
         public void onAnimationEnd(Animation animation) {
                    // The animation has ended, transition to the Main Menu screen
                    startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
                    QuizSplashActivity.this.finish();
         }
         public void onAnimationRepeat(Animation animation) {
         }
         public void onAnimationStart(Animation animation) {
         }
            });
            Log.e("My App","Error Occured in start animating general");
            Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
            LayoutAnimationController controller =
            new LayoutAnimationController(spinin);
            TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
            for (int i = 0; i < table.getChildCount(); i++) {
            TableRow row = (TableRow) table.getChildAt(i);
            row.setLayoutAnimation(controller);
            Log.e("My App","Error Occured in start animating general end ");
            }
         }
         catch(Exception ex)
         {
             Log.e("Exception logged",ex.getStackTrace().toString());

         }
     }

     @Override
     public void onPause() {
         super.onPause();
         TextView logo1 = (TextView) findViewById(R.id.textView1);
         logo1.clearAnimation();

            Log.e("My App","Error Occured in onPause1");

         TextView logo2 = (TextView) findViewById(R.id.textView2);
         logo2.clearAnimation();

            Log.e("My App","Error Occured in onPause1");
         TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
         for (int i = 0; i < table.getChildCount(); i++) {
         TableRow row = (TableRow) table.getChildAt(i);
         row.clearAnimation();
         Log.e("My App","Error Occured in onPause3");
         }
     }
     @Override 
     public void onResume(){
         super.onResume();
         startAnimating();
     }

}

This is Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidbook.triviaquiz"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/quizicon" android:label="@string/app_name" android:debuggable="true">
      <activity android:name=".QuizSplashActivity" android:label="@string/splash">
           <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="QuizGameActivty" android:label="@string/game"></activity>
        <activity android:name="QuizHelpActivity" android:label="@string/help"></activity>
        <activity android:name="QuizMenuActivity" android:label="@string/menu"></activity>
        <activity android:name="QuizScoresActivity" android:label="@string/scores"></activity>
        <activity android:name="QuizsettingsActivity" android:label="@string/settings"></activity>

    </application>

</manifest>

The Exception that is thrown in debug mode:

{com.androidbook.triviaquiz/com.androidbook.triviaquiz.QuizSplashActivity}: java.lang.ClassCastException: android.widget.TextView

Layout File Splash activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="vertical" android:background="@android:color/black" android:layout_weight="0.94" android:weightSum="1">
        <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="0.08" android:text="@string/header" android:textColor="@color/logo_color" android:textSize="24pt" android:id="@+id/textView1"></TextView>
        <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1" android:layout_weight="0.48" android:weightSum="1" android:stretchColumns="*">
            <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_width="fill_parent">
                <ImageView android:src="@drawable/splash1" android:id="@+id/imageView1" android:layout_height="wrap_content" android:layout_width="fill_parent"></ImageView>
                <ImageView android:src="@drawable/splash2" android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
            </TableRow>
            <TableRow android:id="@+id/tableRow2" android:layout_height="wrap_content" android:layout_width="fill_parent">
                <ImageView android:src="@drawable/splash3" android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
                <ImageView android:src="@drawable/splash4" android:id="@+id/imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
            </TableRow>
            <TextView android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="fill_parent" android:textColor="@color/logo_color" android:fadeScrollbars="true" android:focusableInTouchMode="true" android:textSize="24pt" android:text="@string/footer" android:layout_weight="0.36"></TextView>
            </TableLayout>
        <TextView android:layout_width="fill_parent" android:text="@string/version" android:background="@color/version_bkgrd" android:textColor="@color/version_color" android:id="@+id/textView3" android:layout_gravity="center_horizontal" android:layout_height="32dp" android:lineSpacingExtra="@dimen/version_spacing"></TextView>
    </LinearLayout>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2024-12-01 17:52:26

好吧,一切看起来都很好..但我仍然希望您检查两个地方:

  1. 确保您的布局的文件名确实是splash
  2. 如果您之前已将其他名称命名为textView1或textView2,您可能需要清理项目并从头开始重建

Well, everything looks fine..but I still like to have you check two places:

  1. Make sure your layout's file name really is splash
  2. If you have previously named something else as textView1 or textView2, you might want to clean the project and rebuild from scratch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文