我正在遵循 Android 开发者网站上的教程 (教程),但我遇到了这个错误:
下面是我使用导入的文件创建的新活动的代码;非常感谢您提供任何帮助。
我已经进行了大量的研究,似乎大多数人遇到这个问题是因为导入了错误的内容,但我已经导入了FragmentTransaction 中的 add(int, Fragment) 方法不适用于参数 (int, DisplayMessageActivity.PlaceholderFragment)
android.app.Fragment
,这是大多数人解决此错误的方法,并且 DisplayMessageActivity.PlaceholderFragment
是 Fragment 的扩展,所以我认为类型应该匹配得很好(我还尝试将 new PlaceholderFragment()
替换为 new Fragment()
,但仍然提示同样的错误)。我认为这个错误是来自这个文件。下面是我使用导入的文件创建的新活动的代码;非常感谢您提供任何帮助。
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null){
//ERROR IS HERE
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);
}
public static class PlaceholderFragment extends Fragment{
public PlaceholderFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.activity_display_message, container, false);
return rootView;
}
}
}
getSupportManager
写成了getFragmentManager
。 - Squonk