-2

Hi I am fairly new to android development and generally seem to be understanding this, I've been following a tutorial on youtube and it has all worked fine until now I keep getting the error message that the app has stopped unexpectedly.

right now I have two Java files:

  • Game.java
  • MainMenu.java

And I have four XML files:

  • activity_game.xml
  • activity_main_menu.xml
  • pause_menu.xml
  • pause.xml

Here is the code for 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);
    }
}

Here is the code for 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);
    }
}

Here is the code for 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>

Here is the code for 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>

Here is the code for pause_menu.xml:

<?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>

Here is the code for 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>

And here is my manifest file:

<?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>

Sorry I know that's a lot of code but I have spent hours looking at it and cannot find the problem any help is much appreciated.

Log Cat:

 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
Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
randomer
  • 29
  • 4
  • 11

1 Answers1

0

You are getting exception from this line-

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

only probable cause is your font path param "Adventure.ttf" is not correct. make sure you have put the ttf file in assets/Adventure.ttf path of your eclipse project.

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
  • That fixed the start screen but as soon as i move to the next screen it crashes – randomer Apr 06 '15 at 20:35
  • did you do the same mistake there? And please don't get me wrong, you should accept this answer and post another new question. we don't know but that could be another separate problem. For community purpose, we should not ask & answer different problems in comments. Cheers :) – Amit K. Saha Apr 06 '15 at 20:38
  • There was a part of the code that required a higher API than was set on the device this seems to be what caused the second crash – randomer Apr 06 '15 at 20:48
  • thats great to hear that you found your problem :) – Amit K. Saha Apr 06 '15 at 20:48