gpt4 book ai didi

java - Android项目编译不成功(谷歌教程)

转载 作者:太空宇宙 更新时间:2023-11-04 14:40:36 24 4
gpt4 key购买 nike

我正在尝试学习 Android 编程。我找不到它无法编译的原因。

Ant 错误:

-compile:
[javac] Compiling 4 source files to C:\Documents and Settings\Jet\Desktop\My
FirstApp\bin\classes
[javac] C:\Documents and Settings\Jet\Desktop\MyFirstApp\src\com\example\myf
irstapp\DisplayMessageActivity.java:40: error: cannot find symbol
[javac] if (id == R.id.action_settings)
[javac] ^
[javac] symbol: variable action_settings
[javac] location: class id
[javac] C:\Documents and Settings\Jet\Desktop\MyFirstApp\src\com\example\myf
irstapp\DisplayMessageActivity.java:59: error: cannot find symbol
[javac] View rootView = inflater.inflate(R.layout.fragment_dis
play_message,
[javac] ^
[javac] symbol: variable fragment_display_message
[javac] location: class layout
[javac] 2 errors

BUILD FAILED

以下是我的项目的文件。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText android:id="@+id/edit_message"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>

字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>

MainActivity.java

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.*;
import android.view.*;
import android.app.Fragment;
import android.widget.*;
import com.example.myfirstapp.R;

public class MainActivity extends ActionBarActivity
{
/** Called when the activity is first created. */
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

/** 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);
}
}

DisplayMessageActivity.java

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.*;
import android.view.*;
import android.app.Fragment;
import android.widget.*;
import com.example.myfirstapp.R;

public class DisplayMessageActivity extends ActionBarActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

// Set the text view as the activity layout
setContentView(textView);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{

public PlaceholderFragment() { }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}

最佳答案

看起来文件 menu/main.xml 或其调用方式丢失。将定义菜单项settings 的位置。创建一个新项目,您将在 res/menu 文件夹中看到该文件。

关于java - Android项目编译不成功(谷歌教程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988053/

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