Java Android First App 教程问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19075268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android First App Tutorial Issue
提问by user2827799
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. I also have the DisplayMessageActivity in the same folder as MainActivity.
我一直试图让它工作,甚至完全按照教程所说的复制和粘贴代码,但它似乎不起作用。我知道问题出在 MainActivity 或 DisplayMessageActivity 中,但我看不出有什么问题。我在 MainActivity 所在的文件夹中也有 DisplayMessageActivity。
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
显示消息活动
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
更新
Juned and Peter were correct. The only reason it wasn't working right away was because I had messed something else up. Thanks guys!
朱尼德和彼得是正确的。它没有立即工作的唯一原因是因为我把其他东西搞砸了。谢谢你们!
采纳答案by Peter Tretyakov
I had the same problem yesterday (:
You need to add to your imports
in DisplayMessageActivity
我昨天遇到了同样的问题(:你需要添加到你imports
的DisplayMessageActivity
import android.annotation.SuppressLint;
import android.support.v4.app.NavUtils;
Also, you need to add to your build.gradle
file in dependencies
section:
此外,您需要build.gradle
在dependencies
部分中添加到您的文件中:
compile 'com.android.support:support-v4:18.0.+'
About Support Libraries you can red here.
关于支持库,您可以在此处红色。
回答by Juned Ahsan
I don't see the imports for SuppressLint
in your DisplayMessageActivity class. Add the correct imports.
我SuppressLint
在您的 DisplayMessageActivity 类中没有看到导入。添加正确的导入。
Also not that SuppressLint
annotation was added in API level 16. Make sure that you are using build SDK to 16 or higher.
也不SuppressLint
是在 API 级别 16 中添加了注释。确保您使用的是 16 或更高版本的构建 SDK。
回答by bcrochet
The last part of your issue is that you don't have 'package com.example.firstapp;' at the top of DisplayMessageActivity.java.
您问题的最后一部分是您没有“package com.example.firstapp;” 在 DisplayMessageActivity.java 的顶部。
回答by Chamath Jeevan
As for the Android first app documentiaont they have clearly mentioned as below. The Note near the ***
至于Android第一个应用程序文档,他们已经明确提到如下。*** 附近的注意事项
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. Ignore the error for now; you'll create the class soon.
注意:如果您使用的是 Android Studio 等 IDE,则对 DisplayMessageActivity 的引用将引发错误,因为该类尚不存在。暂时忽略错误;你很快就会创建这个类。
Therefore, if you scroll down more in the documentation you can find the topic Create the Second Activitythat create the new DisplayMessageActivity.
因此,如果您在文档中向下滚动更多,您可以找到创建新 DisplayMessageActivity 的创建第二个活动的主题。
回答by Naeem
Add this to your activity_display_message.xml
将此添加到您的 activity_display_message.xml
android:id="@+id/activity_display_message">