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

标签 java android eclipse android-fragments nullpointerexception

我正在开发一个 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/

相关文章:

java - 无法使用 HashMap 替换方法 : call require api level 24 (current min is 22):java. util.HashMap#replace

android - 如何将 android 项目从 API 18 升级到 21

java - 为什么在 LinkedLists 中添加特定索引比在 ArrayLists 中慢

Java 与 JPanel

java - JDBC Derby JPA 持久性测试问题 "Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered ' USER'"

android - OpenCV 检测手和比较图像

java - 我的 Android 应用程序运行非常缓慢

java - Eclipse 没有构建 aidl 文件

java - ShowPhoto 类型中的 createBitmapFromURI(uri) 方法不适用于参数(字符串)

eclipse - 有没有办法从 eclipse 复制代码,包括 ine 数字