[英]Android First App Tutorial Issue
I've been trying to get this to work, even copying and pasting the code exactly as the tutorial says it, but it doesn't seem to work. 我一直试图让它工作,甚至完全按照教程的说法复制和粘贴代码,但它似乎不起作用。 I know the issue is in MainActivity or DisplayMessageActivity, but I can't see what's wrong.
我知道问题出在MainActivity或DisplayMessageActivity中,但我看不出有什么问题。 I also have the DisplayMessageActivity in the same folder as MainActivity.
我还将DisplayMessageActivity与MainActivity放在同一个文件夹中。
I get the following errors. 我收到以下错误。
DisplayMessageActivity
Gradle: error: cannot find symbol class SuppressLint
Gradle: error: package R does not exist
Gradle: error: cannot find symbol variable NavUtils
MainActivity
Gradle: error: cannot find symbol class DisplayMessageActivity
I have been fiddling with this for awhile, and cannot figure out what I am doing wrong. 我已经习惯了一段时间,并且无法弄清楚我做错了什么。 Any help is much appreciated.
任何帮助深表感谢。
What I have, 我有的,
AndroidManifest.xml AndroidManifest.xml中
~snip~
<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>
~snip~
DisplayMessageActivity DisplayMessageActivity
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
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);
}
}
MainActivity 主要活动
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;
}
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);
}
}
UPDATE UPDATE
Juned and Peter were correct. Juned和Peter是对的。 The only reason it wasn't working right away was because I had messed something else up.
它不能立即工作的唯一原因是因为我弄乱了其他东西。 Thanks guys!
多谢你们!
I had the same problem yesterday (: You need to add to your imports
in DisplayMessageActivity
昨天我遇到了同样的问题(:你需要在
DisplayMessageActivity
添加你的imports
import android.annotation.SuppressLint;
import android.support.v4.app.NavUtils;
Also, you need to add to your build.gradle
file in dependencies
section: 此外,您需要添加到
dependencies
部分中的build.gradle
文件:
compile 'com.android.support:support-v4:18.0.+'
I don't see the imports for SuppressLint
in your DisplayMessageActivity class. 我没有在DisplayMessageActivity类中看到
SuppressLint
的导入。 Add the correct imports. 添加正确的导入。
Also not that SuppressLint
annotation was added in API level 16. Make sure that you are using build SDK to 16 or higher. 也不是在API级别16中添加了
SuppressLint
注释。确保您使用的构建SDK为16或更高版本。
The last part of your issue is that you don't have 'package com.example.firstapp;' 问题的最后一部分是你没有'package com.example.firstapp;' at the top of DisplayMessageActivity.java.
在DisplayMessageActivity.java的顶部。
As for the Android first app documentiaont they have clearly mentioned as below. 至于Android第一个应用程序文档,他们已经明确提到如下。 The Note near the ***
***附近的注释
Build an Intent topic , Step 1
构建一个Intent主题,第1步
Note: The reference to DisplayMessageActivity will raise an error if you're using an IDE such as Android Studio because the class doesn't exist yet.
注意:如果您使用的是诸如Android Studio之类的IDE,则对DisplayMessageActivity的引用将引发错误,因为该类尚不存在。 Ignore the error for now;
暂时忽略错误; you'll create the class soon.
你很快就会上课。
Therefore, if you scroll down more in the documentation you can find the topic Create the Second Activity that create the new DisplayMessageActivity. 因此,如果您在文档中向下滚动更多,则可以找到创建创建新DisplayMessageActivity 的第二个活动主题。
将其添加到您的activity_display_message.xml
android:id="@+id/activity_display_message">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.