2015-04-06 6 views
-2

Привет Я довольно новичок в разработке Android и, как правило, понимаю это, я слежу за учебником по youtube, и все работает нормально до сих пор, я продолжаю получать ошибку сообщение о том, что приложение неожиданно остановилось.Eclipse «Извините, приложение неожиданно остановилось»

прямо сейчас у меня есть два Java файлы:

  • Game.java
  • MainMenu.java

И у меня есть четыре XML файлы:

  • activity_game.xml
  • Активность_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> 

Вот код для 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> 

Вот код для 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> 

Извините, я знаю, что это много кода, но я потратил часы, глядя на него, и не могу найти проблему, и любая помощь очень ценится.

Вход 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 
+1

показать нам logcat http://stackoverflow.com/q/23353173/2670892 –

+0

Извините, лог-кот огромен, он не будет публиковать его прямо сейчас. Я выпущу его и запустил проект. – randomer

+0

Я считаю, что вы можете безопасно удалите layout.xml из вашего вопроса. – sschrass

ответ

0

Вы получаете исключение из этой линии-

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

только вероятной причиной является ваш шрифт путь парам "Adventure.ttf" не является правильным. убедитесь, что вы поместили файл ttf в путь assets/Adventure.ttf вашего проекта eclipse.

+0

. Исправлено начальное окно, но как только я перехожу на следующий экран, он сбрасывает – randomer

+0

. Вы сделали там ту же ошибку? И, пожалуйста, не поймите меня неправильно, вы должны принять этот ответ и задать еще один новый вопрос. мы не знаем, но это может быть другой отдельной проблемой. Для целей сообщества мы не должны спрашивать и отвечать на разные проблемы в комментариях. Cheers :) –

+0

Была часть кода, для которой требовался более высокий API, чем был установлен на устройстве. Кажется, это вызвало второй краш – randomer

Смежные вопросы