Adview崩溃我的应用

时间:2017-10-12 21:40:18

标签: android

注意:在我添加广告之前,我的应用程序运行良好,使用相同的方法

当我尝试将adview添加到我的应用时,它会让我的应用崩溃,所以我删除它但仍然给我这个 这是logcat

  

10-12 21:33:19.765 4993-4993 / com.fm360.almorfis E / AndroidRuntime:FATAL EXCEPTION:main                                                                     处理:com.fm360.almorfis,PID:4993                                                                     java.lang.RuntimeException:无法启动活动ComponentInfo {com.fm360.almorfis / com.fm360.almorfis.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.Spinner.setSelection(int) '在空对象引用上                                                                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                                                                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                                                                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                                                                         在android.os.Handler.dispatchMessage(Handler.java:102)                                                                         在android.os.Looper.loop(Looper.java:154)                                                                         在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                                                                      引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.Spinner.setSelection(int)'                                                                         在com.fm360.almorfis.MainActivity.spinnerVal(MainActivity.java:123)                                                                         在com.fm360.almorfis.MainActivity.onCreate(MainActivity.java:59)                                                                         在android.app.Activity.performCreate(Activity.java:6664)                                                                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                                                                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                                                                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                                                                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                                                                         在android.os.Handler.dispatchMessage(Handler.java:102)                                                                         在android.os.Looper.loop(Looper.java:154)                                                                         在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)   这是我告诉的方法

private void spinnerVal(int spinnerID, int move, int selection){
    Spinner sp = (Spinner) findViewById(spinnerID);
    ArrayList<Integer> spinnerOption = new ArrayList<>();
    for (int i = 0; i < 50; i+=move) {
        spinnerOption.add(i);
    }
    ArrayAdapter<Integer> integerArrayAdapter = new ArrayAdapter<>(this, R.layout.spinner_item, spinnerOption);
    integerArrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    sp.setAdapter(integerArrayAdapter);
    sp.setSelection(selection);
    sp.setOnItemSelectedListener(this);

}

1 个答案:

答案 0 :(得分:0)

尝试:

ArrayList<String> spinnerOption = new ArrayList<>();
for (int i = 0; i < 50; i+=move) {
    spinnerOption.add(String.valueOf(i));
}