0

我一直在收到 NullPointerException 错误addSiteButton.setOnClickListener(new View.OnClickListener(){

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

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 布局。

字段.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>

任何帮助表示赞赏!

4

1 回答 1

1

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

于 2013-01-21T17:09:10.280 回答