【问题标题】:NullPointerException Error with ViewPagerViewPager 出现 NullPointerException 错误
【发布时间】:2013-01-21 17:01:12
【问题描述】:

我在 addSiteButton.setOnClickListener(new View.OnClickListener(){ 上遇到 NullPointerException 错误

我一直在试图解决这个问题,但我做不到。我基本上想要做的是单击特定按钮并让它滚动动画到所需的页面。

public class fieldsActivity extends Activity {

Button addSiteButton;
Button cancelButton;
Button signInButton;


/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // to create a custom title bar for activity window
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.fields);
    // use custom layout title bar
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);



    Pager adapter = new Pager();
    final ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);
    mPager.setAdapter(adapter);
    mPager.setCurrentItem(1);

    addSiteButton = (Button) findViewById(R.id.addSiteButton);

    //Error is happening on the line below.
    addSiteButton.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View arg0) {
            mPager.setCurrentItem(2, true);

        }
    });

    cancelButton = (Button) findViewById(R.id.cancel_button);
    signInButton = (Button) findViewById(R.id.sign_in_button);

}

 private class Pager extends PagerAdapter {



    public int getCount() {
        return 3;

    }

    @Override
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
            case 0:
                resId = R.layout.field01;
                break;
            case 1:
                resId = R.layout.add_site;

                break;
            case 2:
                resId = R.layout.main;
                break;

        }

        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeViewAt(arg1);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }
    @Override
    public Parcelable saveState() {
        return null;
    }


}

}

这是 ViewPager 所在的 XML 布局。

fields.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

<android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fieldspager" />


</RelativeLayout>

这是按钮所在的布局。

add_site.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:focusableInTouchMode="false"
            android:background="@drawable/bg"
            android:visibility="visible"
            android:focusable="false"
            android:gravity="center_horizontal">


<Button style="@style/addWebSiteButton"
        android:id="@+id/addSiteButton"
        android:text="Add Website"
        android:layout_marginTop="20dp"
        android:textStyle="normal"
        android:visibility="visible"
        android:singleLine="true"
        android:layout_alignParentTop="false"
        android:layout_alignParentLeft="false"
        android:layout_alignParentRight="false"
        android:clickable="true"
        android:enabled="true"
        android:layout_marginBottom="20dp"
        android:layout_centerHorizontal="true"
        />

<ImageView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:id="@+id/imageView"
        android:layout_below="@+id/addSiteButton"
        android:background="@drawable/line_dividers"
        android:layout_centerHorizontal="true"/>

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_below="@+id/imageView"/>


</RelativeLayout>

感谢任何帮助!

【问题讨论】:

    标签: android nullpointerexception android-viewpager onclicklistener android-button


    【解决方案1】:

    您试图从 fields.xml 获取 Button 的 id,但在 add_site.xml 中声明。 这就是你得到空指针异常的原因。

    【讨论】:

    • 你是对的!我将如何从 add_site.xml 中提取该按钮 ID。我想要这样做的原因是因为我希望能够点击 addSiteButton 并让它滚动到指定的 setCurrentItem。
    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2017-08-09
    • 1970-01-01
    • 2016-06-15
    • 2017-09-28
    相关资源
    最近更新 更多