我遇到了這個麻煩的android:滑動操作與按鈕
package com.app.BoomBase;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.ViewPager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
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.advanced;
break;
case 1:
resId = R.layout.activity_main;
break;
case 2:
resId = R.layout.beginner;
ImageButton ibDistance = (ImageButton) findViewById(R.id.ibDistance);
RelativeLayout RelativeB = (RelativeLayout) findViewById(R.id.RelativeB);
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).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
}
我試圖實現對所謂初學者的活動按鈕,但如果我把一個OnClickListner的情況下,2名爲ibDistance按鈕,程序崩潰
ibDistance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Open Activity here, via Intent
}
});
的logcat的文件:
E/AndroidRuntime(13680): FATAL EXCEPTION: main
E/AndroidRuntime(13680): java.lang.NullPointerException
E/AndroidRuntime(13680): at com.app.BoomBase.MainActivity$MyPagerAdapter.instantiateItem(MainActivity.java:52)
E/AndroidRuntime(13680): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
E/AndroidRuntime(13680): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
E/AndroidRuntime(13680): at android.support.v4.view.ViewPager.populate(ViewPager.java:991)
E/AndroidRuntime(13680): at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
E/AndroidRuntime(13680): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
E/AndroidRuntime(13680): at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime(13680): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
E/AndroidRuntime(13680): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime(13680): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13680): at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime(13680): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(13680): at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
E/AndroidRuntime(13680): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13680): at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime(13680): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(13680): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385)
E/AndroidRuntime(13680): at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)
E/AndroidRuntime(13680): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)
E/AndroidRuntime(13680): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
E/AndroidRuntime(13680): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
E/AndroidRuntime(13680): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
E/AndroidRuntime(13680): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
E/AndroidRuntime(13680): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
E/AndroidRuntime(13680): at android.view.Choreographer.doFrame(Choreographer.java:525)
E/AndroidRuntime(13680): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
E/AndroidRuntime(13680): at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(13680): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(13680): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(13680): at android.app.ActivityThread.main(ActivityThread.java:4921)
E/AndroidRuntime(13680): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13680): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(13680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
E/AndroidRuntime(13680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
E/AndroidRuntime(13680): at dalvik.system.NativeStart.main(Native Method)
我能做些什麼? 謝謝:)
你在哪裏添加了'ibDistance.setOnClickListener'?你可以發佈你的logcat嗎? – AwadKab