2013-08-25 66 views
1

IM'具有下一个问题:无法启动活动componentinfo显示java.lang.NullPointerException阵列喜好

无法启动活动componentinfo显示java.lang.NullPointerException

我做与喜好的节目,这是验证码:

package com.toogle.button; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ToggleButton; 

public class MainActivity extends Activity implements OnCheckedChangeListener { 

EditText editable; 
TextView texto; 
ToggleButton toggle; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    LinearLayout ll = (LinearLayout) findViewById(R.id.ll); 

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    boolean fondo = sp.getBoolean("checkbox", true); 
    if (fondo == true){ 
     ll.setBackgroundColor(Color.BLUE); 

    } 

    String plist = sp.getString("lista", null); **//HERE IS THE PROBLEM** 
    if (plist.equals("1")){ 
      ll.setBackgroundColor(Color.GREEN); 
    } 



    String toast = sp.getString("nombre", null); 
    if(toast.equals("emi")){ 
     Toast t = Toast.makeText(this, "bien", Toast.LENGTH_SHORT); 
     t.show(); 
    } 



    editable = (EditText) findViewById(R.id.etEditable); 
    texto = (TextView) findViewById(R.id.tvTexto); 
    toggle = (ToggleButton) findViewById(R.id.tgToggle); 
    toggle.setOnCheckedChangeListener(this); 

} 



@Override 





public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
    // TODO Auto-generated method stub 
    if (toggle.isChecked()){ 
     editable.setTextColor(Color.RED); 
     editable.setTextSize(30); 
     texto.setText("Activado"); 

    }else{ 
     editable.setTextColor(Color.BLACK); 
     editable.setTextSize(20); 
     texto.setText("Desactivado"); 
    } 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    super.onCreateOptionsMenu(menu); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 


    return true; 
} 



@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    switch(item.getItemId()){ 
    case R.id.iConoce: 
    Intent c = new Intent("com.toogle.button.CONOCE"); 
    startActivity (c); 

     break; 

    case R.id.iPrefs: 
    Intent p = new Intent("com.toogle.button.PREFS"); 
    startActivity (p); 

     break; 

    case R.id.iSalir: 
     finish(); 
     break; 

    } 

    return false; 

} 




} 

的阿雷XML文件:

<?xml version="1.0" encoding="utf-8"?> 

<string-array name="lista"> 
<item>Uno</item> 
<item>Dos</item> 
<item>Tres</item> 
<item>Cuatro</item> 



</string-array> 
<!-- Values --> 
<string-array name="lValores"> 
<item>1</item> 
<item>2</item> 
<item>3</item> 
<item>4</item> 



</string-array> 

而这里的logcat:

08-24 20:28:49.689: E/AndroidRuntime(31419): FATAL EXCEPTION: main 
08-24 20:28:49.689: E/AndroidRuntime(31419): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.toogle.button/com.toogle.button.MainActivity}: java.lang.NullPointerException 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.os.Looper.loop(Looper.java:123) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at java.lang.reflect.Method.invokeNative(Native Method) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at java.lang.reflect.Method.invoke(Method.java:521) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at dalvik.system.NativeStart.main(Native Method) 
08-24 20:28:49.689: E/AndroidRuntime(31419): Caused by: java.lang.NullPointerException 
08-24 20:28:49.689: E/AndroidRuntime(31419): at com.toogle.button.MainActivity.onCreate(MainActivity.java:43) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-24 20:28:49.689: E/AndroidRuntime(31419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
08-24 20:28:49.689: E/AndroidRuntime(31419): ... 11 more 

感谢。

回答

0

既然你在

String plist = sp.getString("lista", null); 

设置字符串null的默认值以下可能导致NullPointerException当您尝试调用一个方法就可以了。

if (plist.equals("1")){ 

尝试使用:

String plist = sp.getString("lista", ""); 

相反。

相关问题