Android 月食“;很抱歉,应用程序意外停止";

Android 月食“;很抱歉,应用程序意外停止";,android,eclipse,android-typeface,Android,Eclipse,Android Typeface,嗨,我是android开发的新手,一般来说似乎理解这一点,我一直在关注youtube上的一个教程,它一直运行良好,直到现在我一直收到应用程序意外停止的错误消息 现在我有两个Java文件: Game.java MainMenu.java 我有四个XML文件: activity_game.xml activity_main_menu.xml pause_menu.xml pause.xml 下面是Game.java的代码: package com.example.deepseadiver;

嗨,我是android开发的新手,一般来说似乎理解这一点,我一直在关注youtube上的一个教程,它一直运行良好,直到现在我一直收到应用程序意外停止的错误消息

现在我有两个Java文件:

  • Game.java
  • MainMenu.java
我有四个XML文件:

  • activity_game.xml
  • activity_main_menu.xml
  • pause_menu.xml
  • pause.xml
下面是Game.java的代码:

package com.example.deepseadiver;

import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Game extends ActionBarActivity {

    View Pause;
    View Pause_Menu;
    RelativeLayout Rel_main_game;

    //Option for user selecting continue
    OnClickListener Continue_List = new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Make the pause menu invisible
            Pause_Menu.setVisibility(View.GONE);
            //Make the game Visible
            Pause.setVisibility(View.VISIBLE);

        }
    };

    //Option for user selecting Main Menu
    OnClickListener Main_Menu_List = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Game.this.finish();

        }
    };


    OnClickListener Pause_Click = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Pause.setVisibility(View.GONE);
            Pause_Menu.setVisibility(View.VISIBLE);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Rel_main_game = (RelativeLayout) findViewById(R.id.Game_Screen);

        //Gets the size of the device Screen
        DisplayMetrics Size = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(Size);

        @SuppressWarnings("unused")
        //Sets the screen Width & Height in pixels
        final int Screen_Height = Size.heightPixels;
        final int Screen_Width = Size.widthPixels;

        //Sets the Pause button Layout
        @SuppressWarnings("static-access")
        LayoutInflater myInflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
        Pause = myInflater.inflate(R.layout.pause, null, false);
        Pause.setX(Screen_Width - 250);
        Pause.setY(0);
        Rel_main_game.addView(Pause);

        Pause.setOnClickListener(Pause_Click);

        //Sets the Height and Width
        Pause.getLayoutParams().height=250;
        Pause.getLayoutParams().width=250;

        Pause = myInflater.inflate(R.layout.pause_menu, null, false);
        Rel_main_game.addView(Pause_Menu);

        Pause_Menu.setVisibility(View.GONE);

        ImageView Continue = (ImageView)Pause_Menu.findViewById(R.id.Continue);
        ImageView Return_Main = (ImageView)Pause_Menu.findViewById(R.id.Return_Main);


        Continue.setOnClickListener(Continue_List);
        Return_Main.setOnClickListener(Main_Menu_List);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
以下是MainMenu.java的代码:

package com.example.deepseadiver;

//Imports the Required Android libaries
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

//Declaration of the Main Menu
public class MainMenu extends ActionBarActivity {

    //Creates the Code Views
    MediaPlayer MainMenuMusic;
    RelativeLayout Start;
    ImageView ImageButton;
    TextView txt;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);

        //Creates a code representation of a graphical object on activity_main_menu
        Start = (RelativeLayout) findViewById(R.id.Button_Start);
        ImageButton = (ImageView) findViewById(R.id.Image_Button);
        txt = (TextView) findViewById(R.id.Text_Start);

        //Imprts a Custom Font
        Typeface Adventure = Typeface.createFromAsset(getAssets(), "Adventure.ttf");
        txt.setTypeface(Adventure);

        //Detects the user touch on Main Menu and changes button appearance
        Start.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }
        });

        //Detects a user Click on Main Menu and starts the next activity
        Start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent Start_Game = new Intent(MainMenu.this, Game.class);
                startActivity(Start_Game);
            }
        });

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
下面是activity_game.xml的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/Game_Screen"
    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="com.example.deepseadiver.Game" >

</RelativeLayout>

以下是activity_main_menu.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:background="@drawable/background"
    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="com.example.deepseadiver.MainMenu" >

    <ImageView
        android:id="@+id/Continue"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/title_image" />

    <RelativeLayout
        android:id="@+id/Button_Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:minHeight="150px"
        android:minWidth="350px" >

        <ImageView
            android:id="@+id/Image_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/button_off" />

        <TextView
            android:id="@+id/Text_Start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center_horizontal"
            android:text="@string/Start"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Rel"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/Continue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/button_off" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="179dp"
                android:text="@string/Continue"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="102dp" >

            <ImageView
                android:id="@+id/Return_Main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:src="@drawable/button_off" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/Return_Main"
                android:layout_centerVertical="true"
                android:layout_marginLeft="178dp"
                android:text="@string/Main"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

以下是pause_menu.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:background="@drawable/background"
    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="com.example.deepseadiver.MainMenu" >

    <ImageView
        android:id="@+id/Continue"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/title_image" />

    <RelativeLayout
        android:id="@+id/Button_Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:minHeight="150px"
        android:minWidth="350px" >

        <ImageView
            android:id="@+id/Image_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/button_off" />

        <TextView
            android:id="@+id/Text_Start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center_horizontal"
            android:text="@string/Start"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Rel"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/Continue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/button_off" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="179dp"
                android:text="@string/Continue"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="102dp" >

            <ImageView
                android:id="@+id/Return_Main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:src="@drawable/button_off" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/Return_Main"
                android:layout_centerVertical="true"
                android:layout_marginLeft="178dp"
                android:text="@string/Main"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

以下是pause.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Pause"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <ImageView
            android:id="@+id/Continue"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/button_off" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/Pause"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

</RelativeLayout>

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.deepseadiver"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <activity
            android:name="MainMenu"
            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="Game"
            android:label="@string/title_activity_game">
        </activity>
    </application>

</manifest>

抱歉,我知道代码太多了,但我已经花了几个小时看了,找不到问题。非常感谢您的帮助

日志类别:

 04-06 20:44:04.300: E/AndroidRuntime(5657): FATAL EXCEPTION: main
04-06 20:44:04.300: E/AndroidRuntime(5657): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deepseadiver/com.example.deepseadiver.MainMenu}: java.lang.RuntimeException: native typeface cannot be made
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.os.Looper.loop(Looper.java:150)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.app.ActivityThread.main(ActivityThread.java:4277)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at java.lang.reflect.Method.invoke(Method.java:507)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at dalvik.system.NativeStart.main(Native Method)
04-06 20:44:04.300: E/AndroidRuntime(5657): Caused by: java.lang.RuntimeException: native typeface cannot be made
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.graphics.Typeface.<init>(Typeface.java:147)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.graphics.Typeface.createFromAsset(Typeface.java:121)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at com.example.deepseadiver.MainMenu.onCreate(MainMenu.java:34)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-06 20:44:04.300: E/AndroidRuntime(5657):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-06 20:44:04.300: E/AndroidRuntime(5657):     ... 11 more
04-06 20:44:04.300:E/AndroidRuntime(5657):致命异常:主
04-06 20:44:04.300:E/AndroidRuntime(5657):java.lang.RuntimeException:无法启动活动组件信息{com.example.deepseadiver/com.example.deepseadiver.MainMenu}:java.lang.RuntimeException:无法创建本机字体
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.os.Handler.dispatchMessage(Handler.java:99)上
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.os.Looper.loop(Looper.java:150)上
04-06 20:44:04.300:E/AndroidRuntime(5657):位于android.app.ActivityThread.main(ActivityThread.java:4277)
04-06 20:44:04.300:E/AndroidRuntime(5657):位于java.lang.reflect.Method.Invokenactive(本机方法)
04-06 20:44:04.300:E/AndroidRuntime(5657):位于java.lang.reflect.Method.invoke(Method.java:507)
04-06 20:44:04.300:E/AndroidRuntime(5657):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-06 20:44:04.300:E/AndroidRuntime(5657):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-06 20:44:04.300:E/AndroidRuntime(5657):在dalvik.system.NativeStart.main(本机方法)
04-06 20:44:04.300:E/AndroidRuntime(5657):原因:java.lang.RuntimeException:无法生成本机字体
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.graphics.Typeface.(Typeface.java:147)
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.graphics.Typeface.createFromAsset(Typeface.java:121)上
04-06 20:44:04.300:E/AndroidRuntime(5657):在com.example.deepseadrover.MainMenu.onCreate(MainMenu.java:34)
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)上
04-06 20:44:04.300:E/AndroidRuntime(5657):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-06 20:44:04.300:E/AndroidRuntime(5657):。。。还有11个

这一行出现异常-

Typeface Adventure = Typeface.createFromAsset(getAssets(), "Adventure.ttf");

唯一可能的原因是字体路径参数“Adventure.ttf”不正确。请确保您已将ttf文件放入eclipse项目的路径中。

显示日志猫对不起,日志猫太大了,现在无法发布。我将清空它,然后重新运行项目。我相信您可以安全地从您的问题中删除布局。xml。它仍然不允许我发布日志猫。它的字符太多。您只需要需要发布在logcat mate中找到的stacktrace,而不是所有的logcat。这修复了开始屏幕,但当我移动到下一个屏幕时,它崩溃了。你在那里犯了同样的错误吗?请不要误解我的意思,你应该接受这个答案,然后再发布一个新问题。我们不知道,但这可能是另一个单独的问题。出于社区目的,我们不应该在评论中询问和回答不同的问题。欢呼:)有一部分代码需要比设备上设置的API更高的API。这似乎是导致第二次崩溃的原因。很高兴听到您发现了问题:)感谢您的帮助,非常感谢:)