新的活动问题

时间:2015-04-14 06:11:33

标签: java android android-activity

好的,所以我终于得到了我的按钮,从主要活动开始我的新活动。现在,我遇到的问题是,当新活动开始时,avd会给我一个错误说"不幸的是,你的应用已经停止了工作"。我试了几个小时才发现问题,直到我开始评论新活动代码的某些部分,最后,我找到了它。请记住,我在新活动中使用的几乎所有变量也都与mainActivity中的变量有共同的名称和ID,但这不应该是问题,因为我没有通过这两个活动传递任何东西...至少现在不是。问题来自纺纱厂。他们的声明工作得很好...我猜,findViewByid方法没有问题,arrayAdapter也没有给出任何问题,但我注意到当我从onItemSelectedListener和setAdapters方法中删除注释时,应用程序才会关闭。我不知道为什么会这样。我使用相同的代码为mainActivity,它完美地工作。 Dunno,如果我重载onCreate方法,但正如我之前所说,它在mainActivity中工作正常。下面给出了onCreate方法的新活动代码......代码的其他部分工作正常。

public class firstYearSecondSemester2 extends Activity implements AdapterView.OnItemSelectedListener {


private String[] grades;
public Spinner spinner0, spinner1, spinner2, spinner3, spinner4, spinner5, spinner6, spinner7, spinner8;
private double[] grade_values = {0.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0};
//private Spinner gpa_grade0, gpa_grade1, gpa_grade2, gpa_grade3, gpa_grade4, gpa_grade5;
private double gpa_grade_0, gpa_grade_1, gpa_grade_2, gpa_grade_3, gpa_grade_4, gpa_grade_5, gpa_grade_6, gpa_grade_7, gpa_grade_8;
private TextView gpa, gpa_credits0, gpa_credits1, gpa_credits2, gpa_credits3, gpa_credits4,gpa_credits5, gpa_credits6, gpa_credits7,gpa_credits8;
private int gpa_credits_0, gpa_credits_1, gpa_credits_2, gpa_credits_3, gpa_credits_4, gpa_credits_5, gpa_credits_6, gpa_credits_7, gpa_credits_8;
public int[] spinner_ids;
public int spinner_check;
public int spinner_index;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_year_second_semester2);

    spinner_ids = new int[9];
    spinner_check = 0;
    spinner_index = 0;
    gpa = (TextView) findViewById(R.id.gpa_results_text);

    grades = getResources().getStringArray(R.array.grades);

    gpa_credits0 = (TextView) findViewById(R.id.gpa_credits_0);
    gpa_credits1 = (TextView) findViewById(R.id.gpa_credits_1);
    gpa_credits2 = (TextView) findViewById(R.id.gpa_credits_2);
    gpa_credits3 = (TextView) findViewById(R.id.gpa_credits_3);
    gpa_credits4 = (TextView) findViewById(R.id.gpa_credits_4);
    gpa_credits5 = (TextView) findViewById(R.id.gpa_credits_5);
    gpa_credits6 = (TextView) findViewById(R.id.gpa_credits_6);
    gpa_credits7 = (TextView) findViewById(R.id.gpa_credits_7);
    gpa_credits8 = (TextView) findViewById(R.id.gpa_credits_8);

    spinner0 = (Spinner) findViewById(R.id.gradeSpinner0);
    spinner1 = (Spinner) findViewById(R.id.gradeSpinner1);
    spinner2 = (Spinner) findViewById(R.id.gradeSpinner2);
    spinner3 = (Spinner) findViewById(R.id.gradeSpinner3);
    spinner4 = (Spinner) findViewById(R.id.gradeSpinner4);
    spinner5 = (Spinner) findViewById(R.id.gradeSpinner5);
    spinner6 = (Spinner) findViewById(R.id.gradeSpinner6);
    spinner7 = (Spinner) findViewById(R.id.gradeSpinner7);
    spinner8 = (Spinner) findViewById(R.id.gradeSpinner8);

    ArrayAdapter<String> each_grade = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, grades);

    each_grade.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner0.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner1.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner2.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner3.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner4.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner5.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner6.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner7.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));
    spinner8.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,/* R.layout.contact_spinner_nothing_selected_dropdown, // Optional*/this));


    spinner0.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner1.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner2.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner3.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner4.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner5.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner6.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner7.setOnItemSelectedListener(firstYearSecondSemester2.this);
    spinner8.setOnItemSelectedListener(firstYearSecondSemester2.this);


}

Logcat消息

   04-14 02:21:16.260    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 11350: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 11356: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 9044: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
04-14 02:21:16.830    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
04-14 02:21:16.910    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-14 02:21:16.910    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 368: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-14 02:21:16.910    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-14 02:21:16.910    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-14 02:21:16.910    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ VFY: unable to resolve virtual method 390: Landroid/content/res/TypedArray;.getType (I)I
04-14 02:21:16.910    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-14 02:21:17.150    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ GC_FOR_ALLOC freed 140K, 8% free 3075K/3340K, paused 0ms, total 5ms
04-14 02:21:17.150    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/dalvikvm-heap﹕ Grow heap (frag case) to 4.195MB for 1127532-byte allocation
04-14 02:21:17.210    1772-1781/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ GC_FOR_ALLOC freed 13K, 7% free 4163K/4444K, paused 3ms, total 4ms
04-14 02:21:18.930    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/Choreographer﹕ Skipped 103 frames!  The application may be doing too much work on its main thread.
04-14 02:21:18.930    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
04-14 02:27:17.189    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 I/Choreographer﹕ Skipped 60 frames!  The application may be doing too much work on its main thread.
04-14 02:27:17.309    1772-1772/com.daftnerds.juliusugochukwu.cgpa4 D/dalvikvm﹕ GC_FOR_ALLOC freed 168K, 7% free 4507K/4796K, paused 2ms, total 2ms
04-14 02:29:34.831    2264-2264/com.daftnerds.juliusugochukwu.cgpa4 D/gralloc_goldfish﹕ Emulator without GPU emulation detected.

现在,一旦我点击按钮开始新活动,就会发生这种情况

04-14 02:30:40.562    2264-2264/com.daftnerds.juliusugochukwu.cgpa4 D/AndroidRuntime﹕ Shutting down VM
04-14 02:30:40.562    2264-2264/com.daftnerds.juliusugochukwu.cgpa4 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0d3cb20)
04-14 02:30:40.602    2264-2264/com.daftnerds.juliusugochukwu.cgpa4 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.daftnerds.juliusugochukwu.cgpa4, PID: 2264
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daftnerds.juliusugochukwu.cgpa4/com.daftnerds.juliusugochukwu.cgpa4.firstYearSecondSemester2}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.daftnerds.juliusugochukwu.cgpa4.firstYearSecondSemester2.onCreate(firstYearSecondSemester2.java:76)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

更改行
spinner0.setAdapter(new NothingSelectedSpinnerAdapter(each_grade,R.layout.contact_spinner_row_nothing_selected,this));

并清理你的项目一次。

答案 1 :(得分:0)

Omg ......发现了问题。确实是第76行。请不要生气。我最初在主要活动中有9个微调器,在新活动中有7个。我复制了主要活动微调器代码并粘贴在新活动中。因此当它到达第8个微调器(第76行)时,它会给出一个错误,因为该微调器在布局中不可用。感谢所有帮助建议的人......真的很感激他们。