OnClickListener显示空指针

时间:2014-04-04 10:48:23

标签: android onclicklistener

**I am developing a remote app, which has lots of buttons in my Activity. i choose three different onclickListeners and onCheckedChangeListener. I am getting a nullpointer exception on first statement when i try to setOnClickListener class to buttons. Need Help..!**

    public void OnCreate(){




if ((getResources().getConfiguration().screenLayout == Configuration.SCREENLAYOUT_SIZE_LARGE) && screenDpi==1) 
{ 
        // yes, we are large and set orientation to landscape
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        setContentView(R.layout.tabletlayout7main);

    screenLayout = 1;

} else if ((getResources().getConfiguration().screenLayout == Configuration.SCREENLAYOUT_SIZE_LARGE)  && screenDpi==1) 

{ 
            // yes, we are xlarge and set orientation to landscape
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.tabletlayout10main);

        screenLayout = 1;   

} else if (screenDpi==0) { 
        // no, we are not / less than large and with less pixels(860)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setContentView(R.layout.activity_maini);
            TextView TviewHome = (TextView) findViewById(R.id.TextViewHome);
            //TviewHome.setMinHeight(45);
            TviewHome.getLayoutParams().height = 50;
            TviewHome.setTextSize((float) 38.0);
} 

        // Buttons block
       //Initialize all the views

        terminal = (Button) findViewById(R.id.dthButtonLock);

       //Channels And Volume buttons for TV tab here

        tvchP = (Button)findViewById(R.id.tvChlPButton);
        tvchM = (Button)findViewById(R.id.tvChM);
        tvvolP = (Button)findViewById(R.id.tvVPButton);
        tvvolM = (Button)findViewById(R.id.tvVolM);
        //Directions TV

        tvOk = (Button)findViewById(R.id.tvOkButton);
        tvDown = (Button)findViewById(R.id.tvDownbutton1);
        tvLeft = (Button)findViewById(R.id.tvleftButton);
        tvRight = (Button)findViewById(R.id.tvRightButton);
        tvUp = (Button)findViewById(R.id.tvUpButton);


   // TV MISC Buttons here

    tvaV = (Button)findViewById(R.id.tvAVButton);
    tvsleep = (Button)findViewById(R.id.tvSleepbutton);
    tvpicAdj = (Button)findViewById(R.id.tvPICbutton6);
    tvEQ = (Button)findViewById(R.id.tvEQZRbutton10);
    tvAsp= (Button)findViewById(R.id.tvASPbutton8);
    tvMenu = (Button)findViewById(R.id.tvMenrButton);
    tvsndAdj = (Button)findViewById(R.id.tvSNDbutton7);
    tvMute = (Button)findViewById(R.id.tvMutButton);
    tvReturn = (Button)findViewById(R.id.buttonDoubleBack);
    tvZoom = (Button)findViewById(R.id.tvZoombutton9);

    //Advanced Controls of TV
    atvFf = (Button)findViewById(R.id.tvFFbutton2);
    atvFr = (Button)findViewById(R.id.tvFRbutton4);
    atvNext = (Button)findViewById(R.id.tvNxtbutton5);
    atvPlay = (Button)findViewById(R.id.tvPlaybutton1);
    atvPause = (Button)findViewById(R.id.tvPausebutton);
    atvPrev = (Button)findViewById(R.id.tvbuttonPrev);
    atvStop = (Button)findViewById(R.id.tvStopbutton3);
    dvdEject = (Button)findViewById(R.id.dvdEjectbutton);

    //Toggle Button Setup for DVD
    tvTg = (ToggleButton) findViewById(R.id.tvToggle);
    dthTg = (ToggleButton) findViewById(R.id.dthToggle);
    dvdTg = (ToggleButton) findViewById(R.id.dvdToggle);

    // DTH Controls...


    dth1 = (Button)findViewById(R.id.dthOneButton);
    dth2 = (Button)findViewById(R.id.dthTwoButton);
    dth3 = (Button)findViewById(R.id.dthThrButton);
    dth4 = (Button)findViewById(R.id.dthFourButton);
    dth5 = (Button)findViewById(R.id.dthFiveButton);
    dth6 = (Button)findViewById(R.id.dthSixButton);
    dth7 = (Button)findViewById(R.id.dthSevenButton);
    dth8 = (Button)findViewById(R.id.dthEightButton);
    dth9 = (Button)findViewById(R.id.dthNineButton);
    dth0 = (Button)findViewById(R.id.dthZeroButton);

    dthMenu = (Button)findViewById(R.id.dthMenubutton);
    dthswap = (Button)findViewById(R.id.dthSwapButton);
    dthMute = (Button)findViewById(R.id.dthbuttonMute);
    dthvolP = (Button)findViewById(R.id.dthViolPButton);
    dthvolM = (Button)findViewById(R.id.dthVolMinusButton);
    dthchP = (Button)findViewById(R.id.dthChPlusButton);
    dthchM = (Button)findViewById(R.id.dthChMinusButton);

    dthBlue = (Button)findViewById(R.id.dthButBlue);
    dthRed = (Button)findViewById(R.id.dthButRed);
    dthGreen = (Button)findViewById(R.id.dthButGreen);
    dthYellow = (Button)findViewById(R.id.dthButYellow);

    dthGuide = (Button)findViewById(R.id.dthbuttonGuide);
    dthFav = (Button)findViewById(R.id.dthbuttonFav);
    dthUp = (Button)findViewById(R.id.dthUpButton);
    dthDown = (Button)findViewById(R.id.dthDownButton);
    dthLeft = (Button)findViewById(R.id.dthLButton);
    dthRight = (Button)findViewById(R.id.dthRButton);
    dthOk = (Button)findViewById(R.id.dthCButton);

    //Listeners Block
    //TV Listeners(Total set of 27) Set One

    tvchP.setOnClickListener(BTCClickListenerOne);
    tvchM.setOnClickListener(BTCClickListenerOne);
    tvvolP.setOnClickListener(BTCClickListenerOne);
    tvvolM.setOnClickListener(BTCClickListenerOne);
    tvOk.setOnClickListener(BTCClickListenerOne);
    tvDown.setOnClickListener(BTCClickListenerOne);
    tvLeft.setOnClickListener(BTCClickListenerOne);
    tvRight.setOnClickListener(BTCClickListenerOne);
    tvUp.setOnClickListener(BTCClickListenerOne);
    tvaV.setOnClickListener(BTCClickListenerOne);
    tvMenu.setOnClickListener(BTCClickListenerOne);

 // TV ADVCD SET TWO

    tvMute.setOnClickListener(BTCClickListenerOne);
    tvsleep.setOnClickListener(BTCClickListenerOne); 
    tvpicAdj.setOnClickListener(BTCClickListenerOne);
    tvsndAdj.setOnClickListener(BTCClickListenerOne);
    tvEQ.setOnClickListener(BTCClickListenerOne);
    tvAsp.setOnClickListener(BTCClickListenerOne);
    tvReturn.setOnClickListener(BTCClickListenerOne);
    tvZoom.setOnClickListener(BTCClickListenerOne);   

 // DTH OnClickListeners(total set of 30)...........

    dth1.setOnClickListener(BTCClickListenerTwo);
    dth2.setOnClickListener(BTCClickListenerTwo);
    dth3.setOnClickListener(BTCClickListenerTwo);
    dth4.setOnClickListener(BTCClickListenerTwo);
    dth5.setOnClickListener(BTCClickListenerTwo);
    dth6.setOnClickListener(BTCClickListenerTwo);
    dth7.setOnClickListener(BTCClickListenerTwo);
    dth8.setOnClickListener(BTCClickListenerTwo);
    dth9.setOnClickListener(BTCClickListenerTwo);
    dth0.setOnClickListener(BTCClickListenerTwo);
    dthMenu.setOnClickListener(BTCClickListenerTwo);
    dthMute.setOnClickListener(BTCClickListenerTwo);
    dthswap.setOnClickListener(BTCClickListenerTwo);

    dthvolM.setOnClickListener(BTCClickListenerTwo);
    dthvolP.setOnClickListener(BTCClickListenerTwo);
    dthchM.setOnClickListener(BTCClickListenerTwo);
    dthchP.setOnClickListener(BTCClickListenerTwo);

    dthUp.setOnClickListener(BTCClickListenerTwo);
    dthDown.setOnClickListener(BTCClickListenerTwo);
    dthOk.setOnClickListener(BTCClickListenerTwo);
    dthLeft.setOnClickListener(BTCClickListenerTwo);
    dthRight.setOnClickListener(BTCClickListenerTwo);

    dthRed.setOnClickListener(BTCClickListenerTwo);
    dthBlue.setOnClickListener(BTCClickListenerTwo);
    dthGuide.setOnClickListener(BTCClickListenerTwo);
    dthGreen.setOnClickListener(BTCClickListenerTwo);
    dthYellow.setOnClickListener(BTCClickListenerTwo);
    dthFav.setOnClickListener(BTCClickListenerTwo);

    //Advanced Controls of TV
    atvFf.setOnClickListener(AdTvListener);
    atvFr.setOnClickListener(AdTvListener);
    atvNext.setOnClickListener(AdTvListener);
    atvPlay.setOnClickListener(AdTvListener);
    atvPause.setOnClickListener(AdTvListener);
    atvPrev.setOnClickListener(AdTvListener);
    atvStop.setOnClickListener(AdTvListener);
    dvdEject.setOnClickListener(AdTvListener);

   //

    tvTg.setOnCheckedChangeListener(OnOffCheckToggle);
    dvdTg.setOnCheckedChangeListener(OnOffCheckToggle);
    dthTg.setOnCheckedChangeListener(OnOffCheckToggle);
}

然后我在我的应用程序中有四种类型的按钮的监听器类

// OnClickListener Interface for TV Button Controls
public OnClickListener BTCClickListenerOne = new OnClickListener() {

//未实现的方法在这里..         }

//OnClickListener Interface for DTH Button Controls
public OnClickListener BTCClickListenerTwo = new OnClickListener() {

//未实现的方法在这里.. }

// Creating a Seperate OnClickListener for Advanced TV Button Controls
public OnClickListener AdTvListener = new OnClickListener() {

//未实现的metod在这里.. }

// Creating a seperate OnCheckedChange Listener for three toggle buttons
public OnCheckedChangeListener OnOffCheckToggle = new OnCheckedChangeListener(){

//未实现的metod在这里.. }

2 个答案:

答案 0 :(得分:2)

您在onCreate

中缺少以下内容
super.onCreate(savedInstanceState);
setContentView(R.layout.<main layout>);

将它放在开头。显然,如果您在提出问题时粘贴了所有内容,则表示您缺少设置内容视图。

答案 1 :(得分:1)

首先设置内容视图。

setContentViewR.layout.yourxml);

然后只有Activity会识别视图和它所在的xml。否则它将通过NullPointer异常。