我用我在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_width="match_parent"
android:layout_height="match_parent"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="72dp"
android:layout_height="72dp"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="72dp"
android:layout_height="72dp"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="72dp"
android:layout_height="72dp"
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_width="72dp"
android:layout_height="72dp"
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_width="72dp"
android:layout_height="72dp"
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
ComponentInfo{com.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!
感谢您的帮助。
答案 0 :(得分:0)
您尚未正确投放图像按钮。
在onCreate()
baslat = (ImageButton)findViewById(R.id.baslatButonu);
ayarlar =(ImageButton) findViewById(R.id.ayarlarButonu);
并在清单
中声明此活动<activity android:name=".Ayarlar">
来自logcat
**java.lang.RuntimeException: Font asset not found fonts/Bariol_Regular.otf**
也添加此字体文件 的 Bariol_Regular.otf 强>