问题描述
我的 AndroidManifest 文件显示错误“未解析的类‘MainActivity’”,但我在包中有一个同名的 java 类和文件,它应该是程序的第一个活动,所以当我尝试运行时程序崩溃了,我检查了我的代码,但没有发现任何问题。
package com.example.tictactoe;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button forward;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forward = (Button)findViewById(R.id.start);
forward.setonClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this,GameUi.class));
}
});
}
@Override
public void onBackpressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to Exit?");
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
finish();
}
});
builder.setNegativeButton("No",int which) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
我的程序使用的第二个活动的java代码(“GameUi.java”)
package com.example.tictactoe
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class GameUi extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_ui);
btn=(Button)findViewById(R.id.backbtn);
btn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(GameUi.this,MainActivity.class));
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tictactoe">
<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/Theme.TicTacToe">
<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=".GameUi">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="#E5E6AD"
tools:context=".MainActivity">
<TextView
android:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_marginBottom="332dp"
android:editable="false"
android:text="Hello Aliens!"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="50dp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="16dp" />
<ImageView
android:id="@+id/Alienimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/heading"
app:srcCompat="@drawable/ic_launcher_foreground" />
<Button
android:id="@+id/Startbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000066"
android:letterSpacing="0.5"
android:padding="0dp"
android:text="Start"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Alienimg"
app:layout_constraintVertical_bias="0.886" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方法
你的清单没问题(我猜你修复了基于 阿卜杜拉·谢赫回答)。问题是因为 MainActivity 中的这一行:
row_num field_name field_value
------- ---------- --------------
1 foo_varchar 'a funny name'
1 bar_int '17'
1 alice_bit '1'
2 foo_varchar 'another name'
2 bar_int '76'
2 alice_bit '0'
...
因为您的按钮 ID 是 forward = (Button)findViewById(R.id.start);
而不是 Startbtn
只需将其替换为:
start
也在 activity_main 和 ImageView 中删除
forward = (Button)findViewById(R.id.Startbtn);
或者为 drawable 找到另一个 src。
,在您的班级中,我没有看到顶部的包裹。 尝试添加它并查看,如果您仍然遇到问题,请尝试失效并重新启动。
Record<string,unknown>
,
这是错误的原因
<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=".GameUi">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
意图过滤器 用于定义哪个 Activity 将成为您的第一个 Activity(当您启动 Android 应用程序时)。您可以在两个 Activity 中使用它,因此应用程序会混淆哪个是您的第一个 Activity,因此它们会出错。
如果 MainActivity 是您的第一个活动
<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=".GameUi">
</activity>