gpt4 book ai didi

java - 为什么我的 fragment 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:19 25 4
gpt4 key购买 nike

我的一侧有一个 ListView,另一侧有一个 fragment 。我正在尝试制作一个简单的应用程序来弄清楚 fragment 是如何工作的。这是我的代码:

主要 Activity :

    public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// storing string resources into Array
//
setContentView(R.layout.activity_main);
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
//List view
final ListView list = (ListView)findViewById(R.id.questionsList);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, R.layout.list_item, adobe_products);

//final TextView text = (TextView)findViewById(R.id.textView1);

list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){

//String selectedFromList =(String) (list.getItemAtPosition(position));
//text.setText(selectedFromList);

}
});

list.setAdapter(adapter);
}

@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;
}


@SuppressLint("ValidFragment")
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment, container, false);
}
}

}

主要 Activity XML 布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<ListView
android:id="@+id/questionsList"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.07" >

</ListView>

<fragment android:name="com.example.Layout.MyFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />



</LinearLayout>

更新:感谢您的帮助,我注意到该 fragment 是一个子类,因此我将其包含在我的 Activity 中。但仍然得到相同的结果。它停止了,甚至没有打开。无论如何,我更新了 logCat 的整个代码。

日志猫:

    02-22 00:06:50.944: E/AndroidRuntime(2886): FATAL EXCEPTION: main
02-22 00:06:50.944: E/AndroidRuntime(2886): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layout/com.example.layout.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
02-22 00:06:50.944: E/AndroidRuntime(2886): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-22 00:06:50.944: E/AndroidRuntime(2886): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-22 00:06:50.944: E/AndroidRuntime(2886): at android.app.ActivityThread.access$600(ActivityThread.java:141)

有什么办法让它发挥作用吗?

最佳答案

我建议您浏览http://developer.android.com/guide/components/fragments.htmlhttp://developer.android.com/training/basics/fragments/creating.html

我在您的代码中发现了许多结构和设计问题,一旦您浏览上面的链接您就会理解它,还可以下载上面链接页面上提供的示例应用程序,如果您仍然无法做到,我一定会尝试用示例代码给出解释。祝你好运

在代码中进行以下更改

1.add the fragment in activity layout like below and don't forget to replace YOUR PACKAGE NAME specified in class attribute 

<fragment
android:id="@+id/article_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
class="YOUR PACKAGE NAME.MyFragment" />

2. Create MyFragment as a separate Class (Should not be inside the Main Activity):

3. Main Activity must extends FragmentAcivity instead of Actvity

4. if you need Listview and fragment side by side change LinearLayout Orientation to android:orientation="horizontal"

它会起作用

关于java - 为什么我的 fragment 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21949079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com