0

我为自定义布局上定义的 3 个复选框设置了 OnCheckedChangeListener,当我运行应用程序时,它会因 logcat 错误而崩溃并强制关闭。

任何帮助将不胜感激,

我的代码如下:

飞溅.java

 public class Splash extends Activity{  
 MediaPlayer ourSong;
 CheckBox checkbox_first ;
 CheckBox checkbox_second ;
 CheckBox checkbox_third ;  
 @Override
  protected void onCreate(Bundle savedInstanceState) {
   this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
   // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);

  setContentView(R.layout.splash);  

  checkbox_first = (CheckBox)findViewById(R.id.checkbox_first);
  checkbox_first.setOnCheckedChangeListener(listener);

  checkbox_second = (CheckBox)findViewById(R.id.checkbox_second);
  checkbox_second.setOnCheckedChangeListener(listener);

  checkbox_third = (CheckBox)findViewById(R.id.checkbox_third);
  checkbox_third.setOnCheckedChangeListener(listener);   
 }
private OnCheckedChangeListener listener = new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {

if(isChecked){
switch(arg0.getId())
  {
    case R.id.checkbox_first:
         if (checkbox_first.isChecked()) {

        Intent intent = new Intent(Splash.this, MainActivity.class);                                     
        startActivity(intent);  
        }
        else{    
                 checkbox_second.setChecked(false);
                 checkbox_third.setChecked(false);
         break;
            }     
    case R.id.checkbox_second:
         if (checkbox_second.isChecked()) {

            setContentView(R.layout.splash);  
            Thread timer = new Thread()
           {
            public void run()
            {
                try
                {
                    sleep(2000); 
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace(); 
                }
                finally
                {
                    Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                    startActivity(intent);  
                 }
              }                          
            };
           timer.start(); 
           }           
else{   
        checkbox_third.setChecked(false);
        checkbox_first.setChecked(false);
         break;
      }              
   case R.id.checkbox_third:

         if (checkbox_third.isChecked()) {        
           ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 
           ourSong.start();
        final Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(2000); 
                    }
                  catch (InterruptedException e){
                    e.printStackTrace(); 
                    }
                  finally{
                  Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                        startActivity(intent); 
                            }
            }}; 

                   timer.start(); 
                        }                      
 else{  
        checkbox_second.setChecked(false);
        checkbox_first.setChecked(false);
      break;}
  }
}

}
};
@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

首选项.java

  public class Prefs extends PreferenceActivity{    
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);
    }
}

MainActivity.java

  public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cool_menu, menu);

getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context,AttributeSet attrs) {

if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);

new Handler().post(new Runnable() {
public void run() {

((TextView) view).setTextSize(25);  
((TextView) view).setTextColor(Color.RED); 
} 
}
);                                        
return view;
}
 catch (InflateException e) {        
 }
  catch (ClassNotFoundException e) {          
  }
}                                  
return null; 
}
}
);           
return super.onCreateOptionsMenu(menu);                                    
}   
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.aboutUs:
        Intent i = new Intent("com.test.demo.ABOUT");
        startActivity(i);
    break;
    case R.id.preferences:
        Intent p = new Intent("com.test.demo.PREFS");
        startActivity(p);
    break;
    case R.id.exit:
        finish();
    break;
}
return false;
  }
  }

xml 文件夹:prefs.xml

<?xml version="1.0" encoding="utf-8" ?> 
  <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
     <Preference 
       android:layout="@layout/checkbox_layout"
       android:key="splash" />
     </PreferenceScreen>

复选框布局.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >   

<CheckBox 
  android:id="@+id/checkbox_first" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textColor="#B22222" 
  android:textSize="25sp" 
  android:text="First" 
  android:onClick="onCheckBoxClicked"
  />       
<CheckBox 
  android:id="@+id/checkbox_second" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textColor="#B22222" 
  android:textSize="25sp" 
  android:onClick="onCheckBoxClicked"
  android:text="Second" 
  />       
<CheckBox 
  android:id="@+id/checkbox_third" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textColor="#B22222"       
  android:textSize="25sp" 
  android:onClick="onCheckBoxClicked"
  android:text="Third" 
 />       
</LinearLayout>

飞溅.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@drawable/splash" /> 

日志猫:

  java.lang.RuntimeException: Unable to start activity 
  ComponentInfo{com.test.demo/com.test.demo.Splash}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
at com.test.demo.Splash.onCreate(Splash.java:26)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

第 26 行表示:

   checkbox_first.setOnCheckedChangeListener(listener);
4

0 回答 0