public class ResultsActivity extends FragmentActivity implements ListFragmentClickable.OnItemSelectedListener{
private ArrayAdapter<String> mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results_view);
//Receive searchTerm from MainActivity
Intent intent = getIntent();
String searchTerm = intent.getStringExtra(MainActivity.SEARCH_TERM);
mAdapter = new ArrayAdapter<String>(this, R.layout.item_label_list);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FactualResponderFragment responder = (FactualResponderFragment) fm.findFragmentByTag("RESTResponder");
if (responder == null) {
responder = new FactualResponderFragment();
ft.add(responder, "RESTResponder");
}
Bundle bundle = new Bundle();
bundle.putString("search_term", searchTerm);
responder.setArguments(bundle);
ft.commit();
}
public ArrayAdapter<String> getArrayAdapter() {
return mAdapter;
}
@Override //creates a DetailFragment when a list item is selected
public void onItemSelected(String link) {
DetailFragment fragment = (DetailFragment) getSupportFragmentManager().findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
} else {
Intent intent = new Intent(getApplicationContext(), PlaceActivity.class);
intent.putExtra(PlaceActivity.EXTRA_URL, link);
startActivity(intent);
}
}
public class ListFragmentClickable extends ListFragment{
private OnItemSelectedListener listener;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_results_view, container, false);
return view;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
updateDetail(); //see bottom
}
public interface OnItemSelectedListener {
public void onItemSelected(String link);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet ListFragmentClickable.OnItemSelectedListener");
}
}
public void updateDetail() {
// Create fake data
String newTime = String.valueOf(System.currentTimeMillis());
// Send data to Activity
listener.onItemSelected(newTime); //should direct PlaceActivity!!
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.blobtag2.ListFragmentClickable" ></fragment>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"></ListView>
</LinearLayout>
最后是日志:
03-21 22:32:03.297: E/AndroidRuntime(764): FATAL EXCEPTION: main
03-21 22:32:03.297: E/AndroidRuntime(764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blobtag2/com.example.blobtag2.ResultsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.os.Looper.loop(Looper.java:137)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-21 22:32:03.297: E/AndroidRuntime(764): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 22:32:03.297: E/AndroidRuntime(764): at java.lang.reflect.Method.invoke(Method.java:511)
03-21 22:32:03.297: E/AndroidRuntime(764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-21 22:32:03.297: E/AndroidRuntime(764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 22:32:03.297: E/AndroidRuntime(764): at dalvik.system.NativeStart.main(Native Method)
03-21 22:32:03.297: E/AndroidRuntime(764): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-21 22:32:03.297: E/AndroidRuntime(764): at com.example.blobtag2.ListFragmentClickable.onCreateView(ListFragmentClickable.java:32)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-21 22:32:03.297: E/AndroidRuntime(764): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.Activity.setContentView(Activity.java:1867)
03-21 22:32:03.297: E/AndroidRuntime(764): at com.example.blobtag2.ResultsActivity.onCreate(ResultsActivity.java:37)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.Activity.performCreate(Activity.java:5008)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-21 22:32:03.297: E/AndroidRuntime(764): ... 11 more
03-21 22:32:03.297: E/AndroidRuntime(764): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f070003, tag null, or parent id 0x0 with another fragment for com.example.blobtag2.ListFragmentClickable
03-21 22:32:03.297: E/AndroidRuntime(764): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
03-21 22:32:03.297: E/AndroidRuntime(764): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-21 22:32:03.297: E/AndroidRuntime(764): ... 30 more
我们非常非常感谢所有的帮助。如果还有什么我可以补充的,请告诉我?
问题是xml上的片段被加载了两次,第二次将其添加到FragmentManager时,您将得到一个IllegalArgumentException。我昨天也遇到了同样的问题。
在我的解决方案中,我更改它以动态地重新创建片段,而不是在xml文件中定义它:
更改此xml文件部分:
<fragment
android:id="@+id/fragment_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.blobtag2.ListFragmentClickable" ></fragment>
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize" />
Fragment fragment = new ListFragmentClickable();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, fragment);
ft.commit();
我想在ListView的项目Click上显示地图...因此,我实现了如下两个片段: 在PlaceHolderFragment.java中,我将设置为: 在MapsFragment.java中,我将地图显示为:
我有一个错误的膨胀到一个布局的片段。我正在使用的IDE是Xamarin,这是一个用编程语言C#开发的android开发工具。 我在这个目录中的类上得到错误 JeugdbewegingApp\JeugdbewegingApp\演示文稿\MainMenu.cs 在下面的代码段中,该错误由片段类名引发。此代码来自NewsFeed。axml布局文件。 我知道命名空间需要以小写形式编写,但您的文件和目录是否
我正在尝试我的手和Android应用程序开发。我目前正在使用Eclipse(我不记得版本,无论最新版本是什么)。我已经跨过了一座桥梁,我似乎无法理解我做错了什么。我正在尝试使用Google Maps V2 API。我浏览了几个文档并尝试了几种技术,所有这些都导致了相同的错误: 我已经连续两天把脸贴在钥匙上,试图弄明白我做错了什么。 我做过的事情: 从一个空白活动开始。 项目- 我试过很多类似问题的
下面是我的布局: 编辑(已解决):最后,我找到了解决方案。应该在JAVA文件mapFragment中添加onDestroyView()方法,如下所示:
我正在写一个android应用程序,其中包括一个谷歌地图片段。当我将地图片段从包含它的活动中排除时,应用程序运行良好。当我包含映射片段时,我会得到下面blockquote中引用的异常。最让人恼火的是,就在几周前,这还没有发生。这个活动运行了几个星期,然后我更新了android studio,现在我得到了这个例外。我已经在google和stackoverflow上搜索了几个解决方案,但没有任何效果。
这里的主要问题是:上面的代码在几乎所有的设备(受刺激的设备,或者一些真实的设备)上都能顺利运行。但当我在三星S3上运行时。它注意到这个错误: 请告诉我如何修复错误,谢谢:)