【问题标题】:"java.lang.NullPointerException" exception in AndroidAndroid 中的“java.lang.NullPointerException”异常
【发布时间】:2013-07-26 13:08:04
【问题描述】:

此 android 活动未运行。谁能帮帮我。它显示以下错误。 当我单击它开始另一个活动时,它不会运行。

我找不到这个错误。

有人能解决吗?

错误: 致命异常:主要

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testingandroid/com.example.testingandroid.SumsMain}: java.lang.NullPointerException
07-26 12:57:22.958: E/AndroidRuntime(791):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

代码

package com.example.testingandroid;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;

public class SelectTitle extends Activity implements android.view.View.OnClickListener{
public int entryNo;
RadioButton r1;RadioButton r2;RadioButton r3;RadioButton r4;
RadioButton r5;RadioButton r6;RadioButton r7;RadioButton r8;
RadioButton r9;RadioButton r10;RadioButton r11;RadioButton r12;
RadioButton r13;
int determiner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_title);
        r1 = (RadioButton) findViewById(R.id.MasteryPack);
        r2 = (RadioButton) findViewById(R.id.Simplification); 
        r3 = (RadioButton) findViewById(R.id.FractionsandRatios);
        r4 = (RadioButton) findViewById(R.id.Number);
        r5 = (RadioButton) findViewById(R.id.Average);
        r6 = (RadioButton) findViewById(R.id.TimeandWork);
        r7 = (RadioButton) findViewById(R.id.TimeandDistance);
        r8 = (RadioButton) findViewById(R.id.Interest);
        r9 = (RadioButton) findViewById(R.id.Measurement);
        r10 = (RadioButton) findViewById(R.id.Percentage);
        r11 = (RadioButton) findViewById(R.id.BoatandTrain);
        r12 = (RadioButton) findViewById(R.id.Practice);

        r1.setOnClickListener(this);
        r2.setOnClickListener(this);
        r3.setOnClickListener(this);
        r4.setOnClickListener(this);
        r5.setOnClickListener(this);
        r6.setOnClickListener(this);
        r7.setOnClickListener(this);
        r8.setOnClickListener(this);
        r9.setOnClickListener(this);
        r10.setOnClickListener(this);
        r11.setOnClickListener(this);
        r12.setOnClickListener(this);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.select_title, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.MasteryPack:
            Intent nextScreen = new Intent(getApplicationContext(), SumsMain.class);

            nextScreen.putExtra("determiner", 1);
            startActivity(nextScreen);
            break;

        case R.id.Simplification:
            Intent nextScreen1 = new Intent(getApplicationContext(), SumsMain.class);
            //String "determiner" = null;
            nextScreen1.putExtra("determiner", 2);
            startActivity(nextScreen1);
            break;

        case R.id.FractionsandRatios:
            Intent nextScreen2 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen2.putExtra("determiner", 3);
            startActivity(nextScreen2);
            break;

        case R.id.Number:
            Intent nextScreen3 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen3.putExtra("determiner", 4);
            startActivity(nextScreen3);
            break;

        case R.id.Average:
            Intent nextScreen4 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen4.putExtra("determiner", 5);
            startActivity(nextScreen4);
            break;

        case R.id.TimeandWork:
            Intent nextScreen5 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen5.putExtra("determiner", 6);
            startActivity(nextScreen5);
            break;

        case R.id.TimeandDistance:
            Intent nextScreen6 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen6.putExtra("determiner", 7);
            startActivity(nextScreen6);
            break;

        case R.id.Interest:
            Intent nextScreen7 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen7.putExtra("determiner", 8);
            startActivity(nextScreen7);
            break;

        case R.id.Measurement:
            Intent nextScreen8 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen8.putExtra("determiner", 9);
            startActivity(nextScreen8);
            break;

        case R.id.Percentage:
            Intent nextScreen9 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen9.putExtra("determiner", 10);
            startActivity(nextScreen9);
            break;

        case R.id.BoatandTrain:
            Intent nextScreen10 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen10.putExtra("determiner", 11);
            startActivity(nextScreen10);
            break;

        case R.id.Practice:
            Intent nextScreen11 = new Intent(getApplicationContext(), SumsMain.class);
            nextScreen11.putExtra("determiner", 12);
            startActivity(nextScreen11);
            break;



        }       
    }

}

【问题讨论】:

  • 发布完整的堆栈跟踪。
  • 另外,发布SumsMainonCreate()。该错误很可能出现在该类中。
  • 可以发R.layout.activity_select_title的内容吗?
  • 我认为当你从 getIntent() 中检索值时应该有问题,所以请发布 SumsMain 类 onCreate() 方法的代码。

标签: android android-activity nullpointerexception


【解决方案1】:

要从一个活动移动到另一个活动,我们通常使用 Intents,我们必须在其中写下我们要移动的类 Activity 的名称。

在您的代码中,您必须在运行代码之前创建一个 SumMain 类。

不需要声明 putExtra 键,你可以直接使用它,就像你声明它一样 整数判断器

【讨论】:

  • 如果是这种情况,异常将是 NoClassDefFound 而不是 NPE。从我们看到的代码来看,putExtra() 并没有导致NPE
【解决方案2】:

您可能正在使用任何尚未创建的对象。检查一下...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    相关资源
    最近更新 更多