gpt4 book ai didi

java - getArguments 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 02:06:14 25 4
gpt4 key购买 nike

我在向 fragment 发送值时遇到问题。

public class ActivityMain extends ActionBarActivity {

int currentPage = 1;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = new Bundle();
textViewPages = (TextView) findViewById(R.id.text_current_page);
if(isOnline()) {
//here will be asyncTask
bundle.putInt("page", currentPage );
fragmentList = new FragmentList();
fragmentList.setArguments(bundle);
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_main, fragmentList);
fragmentTransaction.commit();
}
else {
Toast.makeText(this, getResources().getString(R.string.empty_list), Toast.LENGTH_LONG).show();
}

public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
}

答案可能很简单,但我找不到。

public class FragmentList extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_list, container, false);
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
currentPage = bundle.getInt("page");
}

getInt() 总是抛出 NullPointerException,就像在 onActivityCreated 或 onCreateView 中一样。

activity_main.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:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">

<fragment
android:id="@+id/fragment_main"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="0.9"
android:name="com.example.testex.reader.FragmentList"
tools:layout="@layout/fragment_list">
</fragment>

</LinearLayout>

fragment 列表.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

一些 logcat

06-03 23:57:34.355  25337-25337/com.example.testex.reader E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testex.reader, PID: 25337
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testex.reader/com.example.testex.reader.ActivityMain}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5323)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.testex.reader.FragmentList.onActivityCreated(FragmentList.java:46)
at android.app.Fragment.performActivityCreated(Fragment.java:1708)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1853)
at android.app.Activity.performCreate(Activity.java:5346)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2323)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5323)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

我应该如何更改此代码以在 fragment 中获取 !null 包?

最佳答案

您要添加 FragmentList 两次:

  • 第一次从静态XML中膨胀
  • 第二次使用 FragmentTransaction 动态添加另一个

您添加的第一个 FragmentList 没有参数 - 无法将参数传递给从静态 XML 扩充的 fragment ,正是这个 fragment 引发了异常。

我怀疑您正在寻找的是将 acitvity_main.xml 中的 Fragment 标记替换为 FrameLayout 标记 - FrameLayout 是一个容器,可以容纳动态添加的 fragment 。

关于java - getArguments 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30629668/

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