0

我有一个执行此操作的数组列表:

     ArrayList<Integer> id = new ArrayList<Integer>(); 
     for (int i = 0; i <= 20; i++) {
     id.add(getResources().getIdentifier("q"+i, "raw", getPackageName()));}

这种方法在稍作改动之前效果很好,但现在已经强制关闭了!我得到了这个logcat:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{yaAli.package313.hossein110/yaAli.package313.hossein110.know}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)at android.app.ActivityThread.access$600(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at yaAli.package313.hossein110.know.onCreate(know.java:33)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 12 more

这是我的 OnCreate():

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.basetxt);        
    SharedPreferences   settings=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    ln=settings.getString("LASTREADln", null);
    if(ln.equals("-1")){ln="0";}
    if(ln!=null){
    final ScrollView s1=(ScrollView) findViewById(R.id.sV1);
    s1.post(new Runnable() {@Override
    public void run() {s1.scrollTo(0, Integer.valueOf(ln));} });}

    final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.arza);  

    String pos = getIntent().getStringExtra("key");
    String arr = getIntent().getStringExtra("list");


    TextView tvfa = (TextView)findViewById(R.id.TEXT313);

    String fontPath = "fonts/font1.ttf";
    String fontPath1 = "fonts/font2.ttf";

    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
    Typeface tf1 = Typeface.createFromAsset(getAssets(), fontPath1);

    SharedPreferences sharedpreferences =    PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    tvfa.getRootView().setKeepScreenOn(sharedpreferences.getBoolean("scrnon", false));
    String sizefa=   sharedpreferences.getString("fontsizefa",null);
    String colorfa=   sharedpreferences.getString("fontcolorfa",null);        
    boolean style=   sharedpreferences.getBoolean("appfont", false);
    boolean music=   sharedpreferences.getBoolean("musictype", false);
    boolean curr=   sharedpreferences.getBoolean("currputfont", false);

String t = read(file(pos,arr,null)); {

if (curr){tvfa.setText(PersianReshape.reshape(t));}else{tvfa.setText(t);} // Txt 
tvfa.setTextSize(1, Float.valueOf(sizefa).floatValue());                    // Size
tvfa.setTextColor(Color.parseColor(colorfa));                               // Color
if (style) { tvfa.setTypeface(tf1); }  else {tvfa.setTypeface(tf);}         // Type
if (music) { mp1.start(); } else { mp1.stop(); }  }}                          // Play
//---------------------------------------------------------------------------- 
4

2 回答 2

2

Java 开发的最佳实践是让文字字符串执行 .equals 调用。所以而不是:

var.equals("string")

你做:

"string".equals(var)

这保证了在进行字符串比较时永远不会出现空指针异常。

Also, it looks like you are storing numeric values as strings. Any particular reason you aren't storing them as ints?

于 2012-10-05T16:12:23.653 回答
1

它可能在这里

ln=settings.getString("LASTREADln", null);

这应该是

ln=settings.getString("LASTREADln", "");

因为null如果该键不存在或不包含任何内容,则设置为您的默认值,因此如果它不包含任何内容,您应该将其设置为“”,并且对于您的字符串比较,您应该查找!ln.contentsEquals("")而不是检查它是否为空

从首选项文件中获得的所有字符串也是如此。将默认值设置为""而不是null

于 2012-10-05T16:09:37.987 回答