2013-05-04 62 views
2

我是相当新的Android开发,我想创建与切换到其他活动链接按钮滑动旋转木马,这是我迄今为止...添加多种功能于ViewPager

主的.java

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Remove title bar 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 


    //Sliding Carousel controls 
    MyPagerAdapter adapter = new MyPagerAdapter(); 
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager); 
    myPager.setAdapter(adapter); 
    myPager.setCurrentItem(1); 



} 

//Sliding Carousel controls 
class MyPagerAdapter extends PagerAdapter { 
    public int getCount() { 
     return 5; 
    } 
    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.carousel_1; 
      break; 
     case 1: 
      resId = R.layout.carousel_2; 
      break; 
     case 2: 
      resId = R.layout.carousel_3; 
      Button myButton = (Button) findViewById(R.id.paymybill); 
      myButton.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        Intent intent = new Intent(); 
        setResult(RESULT_OK, intent); 
        finish(); 
       } 

      }); 
      break; 





     case 3: 
      resId = R.layout.carousel_4; 
      break; 
     case 4: 
      resId = R.layout.carousel_5; 
      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; 
    } 

我想获得该按钮的那一刻案例2个工作(但最终所有情况下的将有2至4个按钮)。

当我加载到我的仿真器

   Button myButton = (Button) findViewById(R.id.paymybill); 
      myButton.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        Intent intent = new Intent(); 
        setResult(RESULT_OK, intent); 
        finish(); 
       } 

      }); 
      break; 

注释掉它完美的作品,但是当我包括它崩溃的负载。

我认为问题是我没有正确指向按钮在寻呼机视图和应用程序崩溃,因为main_activity没有任何按钮或布局我引用。

我花了最后2天,通过类似的问题,挖掘,但我不知道我做错了,作为一个例子,this case

v = inflater.inflate(R.layout.dashboard_media, null); 

接缝出现同样的问题,但它使用的是不同的适配器的实用性,我不知道我可以替换什么,我不能(我已经尝试过,没有接缝工作),因为我说我是新来的,我相信这是简单的我很想念!

任何人都可以帮忙吗?

回答

0

理想的情况下,替换以下行

Button myButton = (Button) findViewById(R.id.paymybill); 

Button myButton = (Button) view.findViewById(R.id.paymybill); 

就已经解决了这个问题。

但在你的情况,carousel_3.xml已经按钮paymybill 因此,如何在paymybill按钮添加以下

android:OnClick="payMyBill" 

,并在Java中添加一个函数来处理你想要做

什么
public void payMyBill(View view){ 
    Intent intent = new Intent(); 
    setResult(RESULT_OK, intent); 
    finish(); 
} 
+1

精湛的工作,perfertly,我更新的功能,以......'代码'\t public void payMyBill(View view){ \t意图myIntent = new Intent(view.getContext( ),ccpaymybill.class); \t \t startActivityForResult(myIntent,0); \t}'代码'哪个接缝完成了这项工作。我还测试了第二个按钮和功能,并且(在拉出头发10分钟后,然后意识到我没有将新活动添加到清单中)完美地工作。非常感谢你。 – PaulW 2013-05-06 08:30:34

+0

乐意提供帮助。如果它对你有用,你能接受这个答案吗?谢谢 – 2013-05-06 08:50:20