java - 第一个应用程序(developer.android.com)错误,什么问题?

标签 java android xml eclipse

所以我遵循了 https://developer.android.com/training/basics/firstapp/index.html 上的教程尝试构建一个应用程序,我现在已经编写了所有代码,但出现了一些错误。如果重要的话,我正在使用 Eclipse。

activity_main.xml:我在@string/edit_message 上收到错误 - “错误:错误:找不到与给定名称匹配的资源(在‘提示’处,值为‘@string/edit_message’)。”

和@string/button_send - “错误:错误:找不到与给定名称匹配的资源(在'text'中值为'@string/button_send')。”

    <LinearLayout 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:orientation="horizontal" >
<EditText android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />

</LinearLayout>

然后是 MainActivity.java - 我在 EditText editText = (EditText) findViewById | 上收到错误消息这部分(R.id.edit_message); |说“edit_message 无法解析或不是字段”

    package com.example.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {

    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);


    // Do something in response to button

}
}

接下来,在 strings.xml 上,我得到:“在这一行找到多个注释: - 错误:解析 XML 时出错:格式不正确 (无效标记)“_settings 旁边”> 设置 代码:

    <?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">My First App</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_display_message">Mitt meddelande</string>

</resources>
_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>

然后在 @string/hello_world 上的 activity_display_message.xml 上,我得到“错误:错误:找不到与给定名称匹配的资源(在 'text' 处,值为 '@string/hello_world')。”

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

最后,在 DisplayMessageActivity.java 上,在 (R.layout. | this part - activity_display_message) |我得到“activity_display_message 无法解析或不是字段”

    package com.example.myfirstapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;

public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

抱歉,这篇文章很长而且可能很乱,但我是新手,正在努力学习,所以我真的很想知道这些错误是怎么回事!

提前致谢!

最佳答案

您忘记设置该元素的 ID:

<EditText android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
/>

添加 android:id="@+id/edit_message" 应该可以解决您的问题。

关于java - 第一个应用程序(developer.android.com)错误,什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053616/

相关文章:

java - If 语句显示特定的警报对话框

java - 使用 Apache 从 FTP 下载中截断数据

java - 使用 MOXy 使用默认的 minOccurs 生成模式

javascript - 替换javascript字符串中的欧姆符号(Ω)

xml - 返回 "application/xml"而不是 "text/plain"ASP.NET Core Web API

java - Java 9 jshell 可以用于在另一个 JVM 中运行代码吗?

java - log4j2使用CsvParameterLayout,如何在单元格中添加时间戳?

android - 在 DialogFragment 中单击 "Cancel"按钮后如何启动软键盘?

android - 如何获取安卓手机的电话号码?

android - 如何在android中制作自定义的点状进度条?