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>
任何幫助表示讚賞!
你是對的! 我將如何去從add_site.xml中拉取該按鈕ID。我想這是因爲我想能夠點擊addSiteButton並讓它滾動到指定的setCurrentItem。 – PhDeOliveira