Android首次应用教程问题 [英] Android First App Tutorial Issue
问题描述
我一直在试图得到这个工作,甚至抄袭,准确地粘贴code作为教程说,但它似乎并没有工作。我知道这个问题是MainActivity或DisplayMessageActivity,但我看不出有什么不对。我也有相同的文件夹MainActivity的DisplayMessageActivity。
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.
我收到以下错误。
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
我一直在摆弄这个一段时间,并不能找出我做错了。任何帮助深表AP preciated。
I have been fiddling with this for awhile, and cannot figure out what I am doing wrong. Any help is much appreciated.
我有什么,
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
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);
}
}
更新
Juned和彼得是正确的。它不工作的时候了,唯一原因是因为我搞砸了别的东西了。谢谢你们!
Juned and Peter were correct. The only reason it wasn't working right away was because I had messed something else up. Thanks guys!
推荐答案
我有同样的问题昨天(:
您需要在 DisplayMessageActivity
I had the same problem yesterday (:
You need to add to your imports
in DisplayMessageActivity
import android.annotation.SuppressLint;
import android.support.v4.app.NavUtils;
此外,您还需要添加到您的的build.gradle
文件中的依赖
部分:
compile 'com.android.support:support-v4:18.0.+'
关于支持库可以红这里。
这篇关于Android首次应用教程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!