gpt4 book ai didi

java - Unresolved 类 MainActivity

转载 作者:搜寻专家 更新时间:2023-11-01 03:31:07 25 4
gpt4 key购买 nike

我在使用 Android Studio 时遇到了问题。当我尝试运行任何应用程序时,我收到相同的错误“未找到默认 Activity ”,并且在我的代码行 tools:context=".MainActivity" 中,MainActivity 突出显示为红色并且它说“ Unresolved 类 MainActivity”。即使我创建一个全新的“空 Activity ”,它也会发生。

到目前为止我已经尝试过:

  • 正在刷新 IDE 缓存
  • 检查 Android list 和 MainActivity 中的包名称
  • 选择默认 Activity 应用内配置
  • 确保src是源目录

我还注意到,在我的“最高级”应用中,build.gradle 看起来像这样:

Screenshot

Android list :

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

<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>
</application>

</manifest>

项目目录+代码:

dir

Activity 主要 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">

<ImageView
android:id="@+id/coffee_grains"
android:layout_width="match_parent"
android:layout_height="300sp"
android:contentDescription="Coffee Grains"
android:scaleType="centerCrop"
android:src="@drawable/coffee_grains"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/cup"
android:layout_width="120sp"
android:layout_height="170sp"
android:layout_marginLeft="8dp"
android:src="@drawable/cup"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/coffee_grains" />

<TextView
android:id="@+id/quantity"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_marginLeft="16sp"
android:layout_marginTop="16sp"
android:gravity="center"
android:text="quantity"
android:textAllCaps="true"
android:textSize="16sp"
app:layout_constraintLeft_toRightOf="@id/cup"
app:layout_constraintTop_toBottomOf="@id/coffee_grains" />

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintLeft_toRightOf="@id/cup"
app:layout_constraintTop_toBottomOf="@id/quantity">

<Button
android:id="@+id/plus"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:onClick="increment"
android:text="+" />

<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8sp"
android:layout_marginRight="8sp"
android:gravity="center"
android:text="1"
android:textColor="#000000"
android:textSize="14sp" />

<Button
android:id="@+id/miuns"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:onClick="decrement"
android:text="-" />
</LinearLayout>

<TextView
android:id="@+id/price"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="order summary"
android:textAllCaps="true"
android:textSize="16sp"
app:layout_constraintLeft_toRightOf="@id/cup"
app:layout_constraintTop_toBottomOf="@id/linearLayout" />

<TextView
android:id="@+id/order_summary_text_view"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="0$"
android:textSize="16dp"
app:layout_constraintLeft_toRightOf="@id/cup"
app:layout_constraintTop_toBottomOf="@id/price" />

<Button
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_marginLeft="16sp"
android:layout_marginTop="8sp"
android:gravity="center"
android:onClick="submitOrder"
android:text="order"
android:textSize="16sp"
app:layout_constraintLeft_toRightOf="@id/cup"
app:layout_constraintTop_toBottomOf="@id/order_summary_text_view" />
</android.support.constraint.ConstraintLayout>

主要 Activity 文件:

package com.example.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {

int quantity = 1;
double pricePerCup = 2.90;
String name = "Pawel";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increment(View view) {
quantity = quantity +1;
displayQuantity(quantity);
}
public void decrement(View view) {
quantity = quantity - 1;
displayQuantity(quantity);
}
private String createOrderSummary(double price){
String orderSummary = "Name: " + name + "\nQuantity: " + quantity +
"\nTotal price: $" + price + "\nThanks!";
return orderSummary;
}

private double calculatePrice(int count, double price){
return count*price;
}

/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView =
findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
double totalPrice = calculatePrice(quantity, pricePerCup);
String priceMessage = createOrderSummary(totalPrice);
displayMessage(priceMessage);
}

/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int number) {
TextView quantityTextView = findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}

}

最佳答案

使用这一行 MainActivity XML 文件

tools:context="com.example.justjava.MainActivity"

您不是在 XML 整个 src 目录中引用。

只需从 XML 中删除这一行

tools:context=".folderName.MainActivity"

关于java - Unresolved 类 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com