0

我已经得到的addSiteButton.setOnClickListener(new View.OnClickListener(){NullPointerException异常错误与ViewPager

我一直在试图算出这个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> 

任何帮助表示赞赏!

回答

1

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

+0

你是对的! 我将如何去从add_site.xml中拉取该按钮ID。我想这是因为我想能够点击addSiteButton并让它滚动到指定的setCurrentItem。 – PhDeOliveira

相关问题