我有一个执行此操作的数组列表:
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
//----------------------------------------------------------------------------