我在eclipse中使用navigation-drawer模板来做一个简单的安卓应用程序。我在碎片问题上有点麻烦。我在清单中声明了一个名为PresenceLog片段的片段,但是当我在MainActivity中调用它时,日志仍然说
03-23 13:54:50.817: E/AndroidRuntime(16750): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.singtel.ricecooker/com.singtel.ricecooker.PresenceLogFragment}; have you declared this activity in your AndroidManifest.xml?这是我的舱单
这是我的片段类
public class PresenceLogFragment extends Fragment{
private TextView myText = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.presence_log, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayList<String> userList = null;
RiceServerRequest newRequest = new RiceServerRequest();
//newRequest.getRequestInfo(this);
}
public void updateUserList(ArrayList<String> userList){
LinearLayout lView = (LinearLayout) getView().findViewById (R.layout.presence_log);
//LinearLayout ll = (LinearLayout)fragment.getView().findViewById(R.id.commentFragmentLayout);
for (int i = 0; i < userList.size();i++){
myText = new TextView(getActivity());
myText.setText(userList.get(i));
lView.addView(myText);
}
//setContentView(lView);
}这是我的MainActivity
private void launchPresenceLog(){
Intent intent = new Intent(this,PresenceLogFragment.class);
startActivity(intent);
}如果你知道我的代码有什么问题,那就太好了。另外,由于我是Android程序新手,如果你能推荐一些在线课程的话,我会很感激的。
发布于 2015-03-23 12:46:34
您已经创建了一个片段,因此不能将其称为活动。您需要用片段替换容器视图,适当地替换为FrameLayout。
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new PresenceLogFragment())
.commit();发布于 2015-03-23 12:50:48
你不能通过意图加载碎片。您必须使用片段管理器这样做:
Fragment fragment = new PresenceLogFragment(MainActivity.this);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.yourFragmentContainer, fragment).commit();发布于 2015-03-23 12:34:27
您在AndroidManifest.xml中声明了此活动吗?
查看清单,看看是否有注册了活动的<activity>元素。如果不是,再加一个。
看看这里:http://developer.android.com/guide/topics/manifest/activity-element.html
https://stackoverflow.com/questions/29210295
复制相似问题