2013-04-03 137 views
2

我遇到了這個麻煩的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) 

我能做些什麼? 謝謝:)

+0

你在哪裏添加了'ibDistance.setOnClickListener'?你可以發佈你的logcat嗎? – AwadKab

回答

2

一個問題我要問第一個 - 是按鈕,您嘗試使用您的主要活動的一部分? findViewById通過搜索您在setContentView中提供的視圖來查找您正在查找的視圖 - 在本例中爲按鈕。如果按鈕實際上不在佈局中,那麼它將不會被發現,並會給你一個錯誤。

假設按鈕在主活動佈局這裏是我想是怎麼回事。

通常你需要你的onCreate方法中添加onClickListener。有一些方法可以將onClickListener移動到onCreate以外,例如using a private class。我想你可以解決這個問題有以下幾點:

(1)前宣佈你爲一個實例變量按鈕,您onCreate

ImageButton ibDistance; 

(2)你的onCreate方法

內初始化您的按鈕,並設置 onClickListener
ibDistance = (ImageButton) findViewById(R.id.ibDistance); 
ibDistance.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
       } 
      }); 

如果你在2的情況下佈局變更或添加按鈕(我不能告訴你在做什麼,真的),那麼你就只指您在早些時候宣佈的ImageButton代碼部分。

另一種方式來做到這一點是不使用的onClickListener可言。按鈕都具有onClick XML屬性,該屬性允許您指定觸發的方法,而不必使用onClickListener。要使用這個函數,你需要在你希望按鈕觸發某個事件的活動中聲明一個公共方法。

public void runThisOnClick(View v){ 
    //do something 
} 

評論部分的內容是當您按下按鈕時會發生什麼。很明顯,您可以更改該方法的名稱以及您在該按鈕的onClick屬性中放置的內容以適合您的需求。

+0

該按鈕位於初學者的視圖..這就是爲什麼我試圖把OnClickListner在案例2中。 – Lasse

+0

如果您將OnClik放入此案例中(case2)它說「void」是無效類型對於變量.. – Lasse

+0

正確 - 它是無效的,因爲findViewById查看你提供的視圖,而不是初學者的視圖。初學者是單獨的活動嗎?新的佈局?它如何在應用程序中運行? – Rarw