2016-04-22 48 views
-1

我試了很多時間,但實際上我正在創建一個設置頁我的意思是設置按鈕在哪裏處理應用程序類中的開和關 我從應用程序延伸 它顯示布爾值引用null異常。 以下是清單。布爾顯示空引用異常

   <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:name=".ApplicationClass"> 
    <activity android:name=".SplashScr"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

這是java類。

public class ApplicationClass extends Application { 
MyService.Reciver rec; 
private Context context; 

public Boolean getSoundFX_State() { 
    return soundFX_State; 
} 

public void setSoundFX_State(Boolean soundFX_State) { 
    this.soundFX_State = soundFX_State; 
} 

Boolean soundFX_State; 

@Override 
public void onCreate() 
{ 
super.onCreate(); 


regster(); 



    pndng(); 

    setSoundFX_State(true); 
} 

protected void regster() 
{ 
    try { 
    rec=new MyService().new Reciver(); 
    if(soundFX_State==true) { 

     registerReceiver(
       rec, new IntentFilter(
         "android.intent.action.PHONE_STATE")); 
     Toast.makeText(getBaseContext(), "Your call can disconect automatically", Toast.LENGTH_SHORT) 
       .show(); 


    } 

和setting.java類。

public class Setting extends Activity { 

    MyService.Reciver rec; 
    private CheckBox c1,c2; 
    protected ApplicationClass app; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.setting); 
     app=(ApplicationClass)getApplication(); 
     c1=(CheckBox)findViewById(R.id.checkBox); 
     c2=(CheckBox)findViewById(R.id.checkBox2); 
     rec=new MyService().new Reciver(); 
     if(app.getSoundFX_State()) 
     { 
      c1.setChecked(true); 
     } 
     else { 
      c1.setChecked(false); 
     } 

     c1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

       if(isChecked) { 

        Toast.makeText(getBaseContext(), "c1", Toast.LENGTH_LONG).show(); 
        app.setSoundFX_State(true); 

       } 
       else { 
        app.setSoundFX_State(false); 
        Toast.makeText(getBaseContext(),"off",Toast.LENGTH_SHORT).show(); 

       } 
      } 
     }); 
     c2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       Toast.makeText(getBaseContext(),"c2",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     { 

     }; 

    } 



    public void chkboxclicked(View view) 
    { 

    } 
} 
+1

請發佈異常的堆棧跟蹤。謝謝。 – Pang

+0

你可以發佈你的「setting.xml」佈局文件 –

回答

0

1)變化

Boolean soundFX_State; 

boolean soundFX_State; 

2)

@Override 
public void onCreate() 
{ 
    super.onCreate(); 

    setSoundFX_State(true); 

    regster(); 

    pndng(); 


} 

希望此Wi會有道理的。

+1

謝謝你的工作很好,非常感謝你 –

+0

@princekhan,很高興幫助你。 :) –

+0

@princekhan ,,你能接受嗎? –