【问题标题】:Vogella Android Tutorial, Compiler error in Android StudioVogella Android 教程,Android Studio 中的编译器错误
【发布时间】:2015-08-18 11:46:36
【问题描述】:

我是 Android 开发新手,并遵循 Vogella 使用 Android Studio 进行 Android 开发简介 - 教程位于此处:

http://www.vogella.com/tutorials/Android/article.html#androidstudio_starter

我在步骤 19.4 及以后开始遇到问题。我有与教程中所示完全相同的代码,但 Android Studio 在 MainActivity.java 中显示错误,指出它无法解析符号“常量”并且构建失败并出现编译器错误。我想知道我缺少什么,因为每个步骤都已完成,并且所有代码都与教程中列出的匹配。

MainActivity.java

package com.deluxaur.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;


public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (BuildConfig.DEBUG) {
        Log.d(Constants.LOG, "onCreated called");
    }
    setContentView(R.layout.activity_main);
}

public void onCLick(View view) {
    EditText input = (EditText) findViewById(R.id.main_input);
    String string = input.getText().toString();
    Toast.makeText(this, "Button 1 pressed", Toast.LENGTH_LONG).show();
}

activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/main_input"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:id="@+id/button"
    android:layout_alignLeft="@+id/main_input"
    android:layout_below="@+id/main_input"
    android:layout_marginTop="31dp"
    android:onClick="onClick"
    />

【问题讨论】:

    标签: java android


    【解决方案1】:

    这只是说无法识别常量类。有很多方法可以解决这个问题。

    1. 您可以创建带有静态 LOG 字符串变量的 Constants 类

      public class Constants {
          public static final String LOG = "MyLogTag";
      }
      
    2. 以下代码是可选的,您的程序的功能不需要。它可以帮助提供一些额外的日志,以便您更好地了解应用程序中发生的事情,但这不是必需的。

      //These three lines are optional
      if (BuildConfig.DEBUG) {
         Log.d(Constants.LOG, "onCreated called");
      }
      
    3. 或者您可以简单地更改字符串键。 Log.d() 方法需要两个Strings,第一个用于标记,第二个用于消息。如果您只提供另一个有效的String,它将正常工作。

      if (BuildConfig.DEBUG) {
         Log.d("A KEY", "onCreated called");
      }
      

    【讨论】:

    • 所有 3 个选项都解决了编译和运行应用程序的问题,但我现在注意到 Android Studio 在 MainActivity.java 中显示警告,指出方法 onClick(android.view.view) 从未使用过,并且String string = input.getText().toString(); 行中的变量 string 从未使用过。在运行应用程序时,会显示布局并输入文本字段,但在点击按钮时会崩溃。又是我遗漏了什么还是教程过时了?
    • 您的问题已经超出了该范围,并且答案不会影响任何其他功能。答案已经简单地回答了问题..您可以针对该问题创建一个新帖子.. ^__^.
    • 但是提示你..它警告你,因为它真的没有被调用..它只是一个警告..它不会影响运行时..你可能想在某个地方使用那个方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2013-05-11
    • 2013-05-16
    • 1970-01-01
    相关资源
    最近更新 更多