我一直试图让它工作,甚至完全按照教程的说法复制和粘贴代码,但它似乎不起作用.我知道问题出在MainActivity或DisplayMessageActivity中,但我看不出有什么问题.我还将DisplayMessageActivity与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
我已经习惯了一段时间,并且无法弄清楚我做错了什么.任何帮助深表感谢.
是)我有的,
AndroidManifest.xml中
~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);
}
}
主要活动
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和Peter是对的.它不能立即工作的唯一原因是因为我弄乱了其他东西.多谢你们!
最佳答案
昨天我遇到了同样的问题(:
您需要在DisplayMessageActivity中添加导入
您需要在DisplayMessageActivity中添加导入
import android.annotation.SuppressLint;
import android.support.v4.app.NavUtils;
此外,您需要添加到依赖项部分中的build.gradle文件:
compile 'com.android.support:support-v4:18.0.+'
关于支持库您可以红色here.