gpt4 book ai didi

java - 带有 fragment 和自定义列表的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:49:19 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我从示例中获取了此代码来充当列表。此页面是 3 页 ViewPager 的 fragment Activity 部分。代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class OverviewFragment extends Fragment {

private ViewGroup mContainerView;

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

View rootView = inflater.inflate(R.layout.fragment_overview, container, false);

// Initialize
mContainerView = (ViewGroup)getActivity().findViewById(R.id.container);

// Add some items
addItem();
addItem();
addItem();
addItem();

return rootView;
}

public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu, null);
getActivity().getMenuInflater().inflate(R.menu.activity_overviewfragment, menu);
return true;
}

private void addItem() {
// Instantiate a new "row" view.
final ViewGroup newView = (ViewGroup) LayoutInflater.from(getActivity()).inflate(
R.layout.upcoming_list_item, mContainerView, false);

// Set the text in the new row to a random country.
((TextView) newView.findViewById(android.R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);

// Set a click listener for the "X" button in the row that will remove the row.

/**
newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Remove the row from its parent (the container view).
// Because mContainerView has android:animateLayoutChanges set to true,
// this removal is automatically animated.
mContainerView.removeView(newView);

// If there are no rows remaining, show the empty view.
if (mContainerView.getChildCount() == 0) {
findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
}
}
});
*/

// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newView, 0);
}

private static final String[] UPCOMING = new String[] {
"Apartmen loan -- 200 $", "Car fix invoice -- 300 $", "Internet bill -- 70$", "dinner -- 20$"
};
}

错误:

06-23 13:57:44.440: E/AndroidRuntime(6200): FATAL EXCEPTION: main
06-23 13:57:44.440: E/AndroidRuntime(6200): Process: com.acceleratedcode.apps.myApp, PID: 6200
06-23 13:57:44.440: E/AndroidRuntime(6200): java.lang.NullPointerException
06-23 13:57:44.440: E/AndroidRuntime(6200): at com.acceleratedcode.apps.myApp.OverviewFragment.addItem(OverviewFragment.java:47)
06-23 13:57:44.440: E/AndroidRuntime(6200): at com.acceleratedcode.apps.myApp.OverviewFragment.onCreateView(OverviewFragment.java:27)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)

错误位于 addItem 方法中的某个位置(非常确定)

XML:

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

<ScrollView
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >

<LinearLayout android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

</LinearLayout>

</ScrollView>

</RelativeLayout>

最佳答案

您的问题很可能出现在这一行:

 ((TextView) newView.findViewById(android.R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);

将其更改为:(假设布局中的 TextView 的 ID 为“text1”)

 ((TextView) newView.findViewById(R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);

您引用的 ID 已被 Android 占用,并且(可能)无法在您的布局文件中找到。

除此之外:

尝试使用 View 而不是 ViewGroup 作为“newView”。并为“mContainerView”使用 LinearLayout 而不是 ViewGroup

关于java - 带有 fragment 和自定义列表的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24365370/

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