我开始学习如何通过开发者页面上的教程来制作Android应用程序。我正在尝试创建一个新的DisplayMessageActivity类。 这是教程https://developer.android.com/training/basics/firstapp/starting-activity.html#CreateActivity
我的DisplayMessageActivity.java文件默认包含:
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
@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);
}
}
虽然在他们的网站上它应该包含这个:
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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;
}
}
}
我已经将我的eclipse更新到最新版本的ADT插件,我仍然得到了这个。当我尝试将他们的代码粘贴到我的DisplayMessageActivity.java文件时,我得到这些错误:
activity_display_message无法解析或不是字段
FragmentTransaction类型中的方法add(int,Fragment)不是 适用于参数(int, DisplayMessageActivity.PlaceholderFragment)
fragment_display_message无法解析或不是字段
答案 0 :(得分:0)
代码中的两个引用都应该是R.layout.activity_display_message,尽管我没有遇到任何错误(请确保您遵循了教程中的所有步骤)。但是我遵循相同的教程并且使用add方法得到了相同的错误,并且无法找出原因。