gpt4 book ai didi

java - 为什么这个 fragment 没有得到 bundle ?

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

我正在制作一个简单的应用程序来学习使用 fragment 。我想将一个字符串从一个 Activity 传输到一个 fragment 。我是通过 Bundle 做到的,但它不起作用。在 fragment 类中,它不接受代码并将其突出显示为无法访问的代码。这是我的代码:主要 Activity : 公共(public)类 MainActivity 扩展 FragmentActivity {

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


Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("key", selectedFromList);
fragment.setArguments(bundle);

}
});

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


}

主要 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="horizontal"
tools:context=".MainActivity" >

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

</ListView>

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

</LinearLayout>

fragment :

    public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View myFragmentView = inflater.inflate(R.layout.activity_fragment, container, false);

Bundle bundle = getArguments();
String selected = bundle.getString("key");

TextView text = (TextView) myFragmentView.findViewById(R.id.text);
text.setText(selected);


// Inflate the layout for this fragment

return myFragmentView;



}
}

更新:我刚刚进行了更改,不再出现此错误,但它仍然无法打开应用程序。它在加载时停止。这是catLog:

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

最佳答案

将 return 放在底部,return 表示我已完成并完成此方法和 fragment 。

 private TextView text;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


text = (TextView) myFragmentView.findViewById(R.id.text);

// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment, container, false);


}

您的 bundle 操作代码将不起作用,您创建了一个新的 Fragment 实例,但仅此而已。应该做的是:

在 MyFragment 类中放置一个方法,例如

public void setMyText(String s){

text.setText(s);
}

并在点击监听器中,

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

String selectedFromList =(String) (list.getItemAtPosition(position));


MyFragment f = (MyFragment) getSupportFragmentManager()
.findViewById(R.id.article_fragment);
f.setMyText(selectedFromList);

}
});

关于java - 为什么这个 fragment 没有得到 bundle ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956147/

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