当我按下一个按钮时,“不幸的是我的应用程序已停止”[重复]

Posted

技术标签:

【中文标题】当我按下一个按钮时,“不幸的是我的应用程序已停止”[重复]【英文标题】:"Unfortunately MyApp has stopped" when I Press A Button [duplicate] 【发布时间】:2018-11-04 20:58:23 【问题描述】:

我用我在 github 上找到的代码制作了一个问答游戏,因为我是新手,我想学习编程。 所以,当我按下开始按钮游戏崩溃。我在没有其他课程的情况下尝试过,但它仍然崩溃。有我的 MainActivity 类,MainActivity xml 和 androidManifest。

MainActivity.java

package com.proje.bilgiyarismasi.bilgiyarismasi;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;


public class MainActivity extends AppCompatActivity 
ImageButton baslat,ayarlar;

@Override
protected void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    baslat = findViewById(R.id.baslatButonu);
    ayarlar = findViewById(R.id.ayarlarButonu);
    baslat.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Intent intent = new Intent(MainActivity.this, SoruAlani.class);
            startActivity(intent);
            finish();

        

    );
    ayarlar.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Intent intent = new Intent(MainActivity.this,Ayarlar.class);
            startActivity(intent);
            finish();
        
    );



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@color/deepPurple"
tools:context="com.proje.bilgiyarismasi.bilgiyarismasi.MainActivity"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp">


<TextView
    android:id="@+id/logoIsim"
    android:layout_
    android:layout_
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="56dp"
    android:text="@string/logoIsim"
    android:textColor="@color/orenji"
    android:textSize="50sp"
    tools:layout_editor_absoluteX="150dp"
    tools:layout_editor_absoluteY="28dp" />

<ImageButton
    android:id="@+id/baslatButonu"
    android:layout_
    android:layout_
    android:layout_below="@+id/logoIsim"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="120dp"
    app:srcCompat="@drawable/menu_1"
    android:adjustViewBounds="true"
    />



<ImageView
    android:id="@+id/playButton"
    android:layout_
    android:layout_
    app:srcCompat="@drawable/play_button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignBottom="@+id/baslatButonu"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginBottom="2dp"
    android:gravity="center"/>


<TextView
    android:id="@+id/basla"
    android:layout_
    android:layout_
    android:layout_alignBottom="@id/baslatButonu"
    android:layout_alignEnd="@id/baslatButonu"
    android:layout_alignLeft="@id/baslatButonu"
    android:layout_alignRight="@id/baslatButonu"
    android:layout_alignStart="@id/baslatButonu"
    android:layout_alignTop="@id/baslatButonu"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="@string/baslat"
    android:textAlignment="center"
    android:textSize="30sp"
    android:textStyle="bold"
    />

<ImageButton
    android:id="@+id/ayarlarButonu"
    android:layout_
    android:layout_
    android:layout_below="@+id/baslatButonu"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    app:srcCompat="@drawable/menu_1"
    android:adjustViewBounds="true"

    />

<TextView
    android:id="@+id/ayarlar"
    android:layout_
    android:layout_
    android:layout_alignBottom="@id/ayarlarButonu"
    android:layout_alignEnd="@id/ayarlarButonu"
    android:layout_alignLeft="@id/ayarlarButonu"
    android:layout_alignRight="@id/ayarlarButonu"
    android:layout_alignStart="@id/ayarlarButonu"
    android:layout_alignTop="@id/ayarlarButonu"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="@string/ayarlar"
    android:textAlignment="center"
    android:textSize="30sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/ayarla"
    android:layout_
    android:layout_
    android:layout_alignBottom="@id/ayarlarButonu"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginBottom="2dp"
    app:srcCompat="@drawable/settings_button"
    android:adjustViewBounds="true"
    />

<TextView
    android:id="@+id/sosyalMedya"
    android:layout_
    android:layout_
    android:layout_below="@+id/ayarlarButonu"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/sosyalMedya"
    android:textAlignment="center"
    android:textStyle="bold"
    android:gravity="center"/>

<ImageButton
    android:id="@+id/facebookLogo"
    android:layout_
    android:layout_
    android:layout_alignTop="@id/sosyalMedya"
    app:srcCompat="@drawable/facebook_logo"
    android:adjustViewBounds="true"
    android:background="@null"
    android:scaleType="fitCenter"
    android:layout_toLeftOf="@id/sosyalMedya"
    android:layout_toStartOf="@id/sosyalMedya"
    android:layout_marginTop="30dp"


    />

<ImageButton
    android:id="@+id/twitterLogo"
    android:layout_
    android:layout_
    android:layout_alignTop="@+id/sosyalMedya"
    app:srcCompat="@drawable/twitter_logo"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:background="@null"
    android:layout_centerInParent="true"
    android:layout_marginTop="30dp"

    />

<ImageButton
    android:id="@+id/slackLogo"
    android:layout_
    android:layout_
    app:srcCompat="@drawable/slack_logo"
    android:layout_alignTop="@+id/sosyalMedya"
    android:adjustViewBounds="true"
    android:background="@null"
    android:scaleType="fitCenter"
    android:layout_toRightOf="@id/sosyalMedya"
    android:layout_toEndOf="@id/sosyalMedya"
    android:layout_marginTop="30dp"
    />

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.proje.bilgiyarismasi.bilgiyarismasi">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


<activity android:name=".TekrarOyna" />
<activity android:name=".OyunKazanma" />
<activity android:name=".SoruAlani" />
<activity android:name=".SureBitti">

</activity>
</application>


</manifest>

logcat

 05-25 12:28:18.414 6393-6393/com.proje.bilgiyarismasi.bilgiyarismasi 
    E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.proje.bilgiyarismasi.bilgiyarismasi, PID: 6393

    java.lang.RuntimeException: Unable to start activity 

                   ComponentInfocom.proje.bilgiyarismasi.bilgiyarismasi/com.proje.bilgiyarismasi.bilgiyarismasi.SoruAlani:
 java.lang.RuntimeException: Font asset not found fonts/Bariol_Regular.otf

    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)

    at android.app.ActivityThread.access$800(ActivityThread.java:151)

    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

    at android.os.Handler.dispatchMessage(Handler.java:102)

    at android.os.Looper.loop(Looper.java:135)

    at android.app.ActivityThread.main(ActivityThread.java:5254)

    at java.lang.reflect.Method.invoke(Native Method)

    at java.lang.reflect.Method.invoke(Method.java:372)

    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)



    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

    Caused by: java.lang.RuntimeException: Font asset not found 
    fonts/Bariol_Regular.otf

    at android.graphics.Typeface.createFromAsset(Typeface.java:190)

   at com.proje.bilgiyarismasi.bilgiyarismasi.SoruAlani.onCreate(SoruAlani.java:51)

   at android.app.Activity.performCreate(Activity.java:5990)

   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)

   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)

   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 

   at android.app.ActivityThread.access$800(ActivityThread.java:151) 

   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 

   at android.os.Handler.dispatchMessage(Handler.java:102) 

   at android.os.Looper.loop(Looper.java:135) 

   at android.app.ActivityThread.main(ActivityThread.java:5254) 

   at java.lang.reflect.Method.invoke(Native Method) 

   at java.lang.reflect.Method.invoke(Method.java:372) 



  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 

   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
   05-25 12:28:20.016 4207-6527/com.android.providers.calendar E/SQLiteLog: 
   (284) automatic index on view_events(_id)
   05-25 12:28:26.129 1535-1602/system_process E/InputDispatcher: channel 
   '2cf4e81e 
 com.proje.bilgiyarismasi.bilgiyarismasi/com.proje.bilgiyarismasi.bilgiyarismasi.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

感谢您的帮助。

【问题讨论】:

只添加logcat的异常部分 尝试在 playButton 中从 android:layout_alignBottom="@+id/baslatButonu" 中删除 + 符号 学习调试,youtube上有很多视频。学习为无法调试的错误发布崩溃 logcat。 阅读此内容以获取 +id 和 id ***.com/a/5025971/8089770 【参考方案1】:

您没有正确投射图像按钮。

onCreate()中使用它

    baslat = (ImageButton)findViewById(R.id.baslatButonu);
    ayarlar =(ImageButton) findViewById(R.id.ayarlarButonu);

并在清单中声明此活动

<activity android:name=".Ayarlar">

来自日志猫

**java.lang.RuntimeException: Font asset not found fonts/Bariol_Regular.otf**

也添加这个字体文件 Bariol_Regular.otf

请检查此链接 How to use custom font in Android Studio

【讨论】:

在新的 android studio 中不需要铸造。我认为活动名称是 MAinActivity 不是吗? 是的,你是对的,不能说@Kangaetemo 正在使用 In 3.0 with sdk 26 :) 对不起,我忘了补充说我使用的是 2.3.3。感谢您的回答,但游戏仍然崩溃,我在 logcat 上看不到任何新内容 java.lang.RuntimeException: Unable to start activity 如果您还没有添加,请在清单中声明此活动 java.lang.RuntimeException: 找不到字体资源 fonts/Bariol_Regular.otf

以上是关于当我按下一个按钮时,“不幸的是我的应用程序已停止”[重复]的主要内容,如果未能解决你的问题,请参考以下文章

当我按下返回按钮时应用程序没有响应

当我按下特定按钮时关闭上一个窗口[重复]

当我按下快速播放按钮时,声音播放失败

当我按下按钮时没有调用方法

当我按下后退按钮 Swift 3 时,删除的数据重新出现

当我按下下载按钮时如何显示带有 CircularProgressIndicator 和下载状态的弹出窗口