0

我在片段中有很多次按钮,但由于某种原因,我不断收到空指针。

我很茫然

片段活动

public class WelcomeBase extends FragmentActivity {

    FragmentAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

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

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new FragmentAdapter(getSupportFragmentManager(), getApplicationContext());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }


}

分段

public class FragmentThree extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    private String mContent = "Page3";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(ARG_SECTION_NUMBER)) {
            mContent = savedInstanceState.getString(ARG_SECTION_NUMBER);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.welcome_page3, container, false);
        Button close = (Button) root.findViewById(R.id.close_helper);
        close.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });
        return root;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(ARG_SECTION_NUMBER, mContent);
    }
}

这是片段 xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transparent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:baselineAligned="false"
    android:textAlignment="gravity"
    android:gravity="center_vertical"
    android:clickable="true">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/welcome_close"
        android:layout_gravity="center" />
</LinearLayout>

空指针在这一行

    close.setOnClickListener(new View.OnClickListener() {

我尝试从 xml 布局实现 onclick,我尝试从 fragmentactivity 实现 onclicklistener,但没有任何效果。我到底做错了什么?

我正在使用 Android Studio

4

1 回答 1

0

请检查片段和 xml 文件中使用的关闭按钮的 ID。

于 2013-10-23T03:43:20.083 回答