Android 以下代码上的Null指针异常

Android 以下代码上的Null指针异常,android,Android,我是android和java的初学者,谁能帮我在我的代码plz中找出这个错误的原因 serviceSwitch =(Switch) this.findViewById(R.id.switchservice); serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView, boolean isCh

我是android和java的初学者,谁能帮我在我的代码plz中找出这个错误的原因

serviceSwitch =(Switch) this.findViewById(R.id.switchservice);
serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
        try{
            if(isChecked){
                arrayChecking();
                Toast.makeText(getApplicationContext(), "set date n time ", Toast.LENGTH_LONG).show();                  
            }
        }
        catch(Exception e){
            Log.e("switchfailed",e.toString());
        }
    }       
});
public void arrayChecking(){

    if(tbPreferenceArray.length>0){
        Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
        serviceSwitch.setChecked(false);        
    }
    else{
        Intent i = new Intent(MainActivity.this, TimerService.class);
        MainActivity.this.startService(i);
        Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();   
    }
}
阵列检查代码 它会检查tbpreferenceArray是否为空?如果为空,则显示toast,否则会传递在我的应用程序中调用后台服务的意图

public void arrayChecking(){

    if(tbPreferenceArray.length>0){
        Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
        serviceSwitch.setChecked(false);        
    }
    else{
        Intent i = new Intent(MainActivity.this, TimerService.class);
        MainActivity.this.startService(i);
        Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();   
    }
}
共享首选项代码

public void arrayChecking(){

    if(tbPreferenceArray.length>0){
        Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
        serviceSwitch.setChecked(false);        
    }
    else{
        Intent i = new Intent(MainActivity.this, TimerService.class);
        MainActivity.this.startService(i);
        Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();   
    }
}
SharedPreferences prefs = getApplicationContext().getSharedPreferences("TogglePreference", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt("tbPreferenceArray" +"_size", toggleButtonArray.length); 
    for(int i=0;i<toggleButtonArray.length;i++)
    {
        editor.putBoolean("tbPreferenceArray" + "_" + i, toggleButtonArray[i]);

    }
    editor.commit();
    Toast.makeText(getApplicationContext(), "Your timetable is ok",Toast.LENGTH_LONG).show();
SharedReferences prefs=getApplicationContext().GetSharedReferences(“TogglePreference”,0);
SharedReferences.Editor=prefs.edit();
putInt(“tbPreferenceArray”+“_size”,toggleButtonArray.length);

对于(int i=0;i请按如下方式更正代码:

public void arrayChecking(){

    if(tbPreferenceArray.length>0){
        Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
        serviceSwitch.setChecked(false);        
    }
    else{
        Intent i = new Intent(MainActivity.this, TimerService.class);
        MainActivity.this.startService(i);
        Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();   
    }
}
if(tbPreferenceArray!= null && tbPreferenceArray.length>0)

请使用“arrayChecking()”代码更新您的帖子并添加logcat错误信息我可以看到超过个变量将为null,但您必须检查logcat。将e.printStackTrace()添加到catch块并再次发布logcat输出请发布arrayChecking();方法以及属于该方法的所有必要代码。您是否在活动或片段中使用此代码