gpt4 book ai didi

java - getArgument() 始终为 null

转载 作者:行者123 更新时间:2023-12-02 02:58:32 25 4
gpt4 key购买 nike

我知道这个问题已经被问了一遍又一遍,但我无法做到这一点。

我只想将参数从 Activity 传递到 Fragment。我已经尝试了网上能找到的所有方法,但无法使其工作。我总是收到 NullPointerException 错误。我什至尝试过测试代码,结果相同。所以这里是:

MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Bundle bundle = new Bundle();
bundle.putString("my_key", "My String");
MyFragment myFrag = new MyFragment();
myFrag.setArguments(bundle);
}
}

MyFragment.java

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentlayout, container, false);
TextView text = (TextView) view.findViewById(R.id.game);
String getArgument = getArguments().getString("my_key");
text.setText(getArgument);
return view;
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.test.MainActivity">

<fragment
android:id="@+id/fragment_game"
class="com.example.test.test.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:layout="@layout/fragmentlayout" />

</FrameLayout>

fragmentlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MyFragment" >

<TextView
android:id="@+id/game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#411124"
/>
</LinearLayout>

错误

FATAL EXCEPTION: main
Process: com.example.test.test, PID: 11917
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test.test/com.example.test.test.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.test.test.MyFragment.onCreateView(MyFragment.java:20)
at android.app.Fragment.performCreateView(Fragment.java:2353)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:959)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1149)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1251)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2311)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5901)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
at android.app.Activity.setContentView(Activity.java:2414)
at com.example.test.test.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

如何获取参数而不报错?

最佳答案

您的代码有 2 个问题,a) 您正在实例化 Fragment以编程方式但您没有将其附加到 Activity ,所以它完全没用,并且b)您还实例化了 Fragment在 xml 布局中 Fragment将没有机会在 onCreateView 之前设置参数叫做。因此,如果需要删除Fragment从 xml 中,在 Activity.onCreate 中创建一个实例,设置参数,然后将其附加到 ActivityFragmentManager

更改activity_main.xml对此(请注意 android:id 属性)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.test.MainActivity">

</FrameLayout>

以及onCreateMainActivity

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = new Bundle();
bundle.putString("my_key", "My String");
MyFragment myFrag = new MyFragment();
myFrag.setArguments(bundle);
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, myFrag, "myFrag")
.commit();

关于java - getArgument() 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721233/

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