【问题标题】:Startactivity for result with causes crash导致崩溃的结果的 Startactivity
【发布时间】:2019-12-24 21:23:36
【问题描述】:

我正在尝试学习 Android Studio,我的第一个应用是血液酒精计算器。用户启动该应用程序,然后启动一个新活动,以便他们可以输入他们的体重并按确定,这会将他们返回到主活动并填写体重文本。

我在第二个活动中使用startActivityForResult,然后使用putExtra。如果我使用 getExtra 方法,第一个活动会崩溃,如果我删除 2 行接收代码,则不会崩溃。当我使用调试器时,它会在显示 App 已停止工作之前显示 NullPointerException

主要活动代码

    public class MainActivity extends Activity {
      static int displayunit = 0;
      static double percentage = 5.0;
      static int change = 1;
      static double bah;
      static double vol = 25;
      static double timestamp = 0;
      static double w = 0;
      static String we = "a";
      final int rcode = 3;
      final String[] units = {"Small Shot(25ml)", "Large Shot(35ml)", "Small 
    Port/Sherry(50ml)", "Large Port/Sherry(70ml)", "Small Wine(125ml)", 
    "Large 
    Wine(175ml)", "Small Beer Bottle(284ml)", "Half Pint(236.6ml)", "Medium 
    Beer Bottle(330ml)", "Can of beer(440ml)", "Large Bottle(500ml)", 
    "Pint(568.26ml)", "Massive Beer Bottle(660ml)"};
    final int[] unitsum = {25, 35, 50, 70, 125, 175, 284, 237, 330, 440, 500, 
    569, 660};

      @Override

      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();





 intent.setClassName("com.example.alccalc","com.example.alccalc.enterweight");
            if(w ==0){
             startActivityForResult(intent, rcode);
            }

      }

            @Override
            protected void onActivityResult ( int requstCode, int resultCode, 
    Intent intent){
                if (requstCode == rcode && resultCode == RESULT_OK) {
                    we = getIntent().getStringExtra("weighttext");
                    w = Double.parseDouble(we);
                }
                TextView kg = (TextView) findViewById(R.id.kg);
                kg.setText(we)

Second Activity

    public class enterweight extends Activity {

    EditText entweight;
    TextView tester;
    String weightstring;

    @Override
    protected void onCreate(Bundle State) {
    super.onCreate(State);
    setContentView(R.layout.activity_enterweight);

    entweight = (EditText) findViewById(R.id.entweight);
    tester = (TextView)findViewById(R.id.tester);


    Button okweight = (Button) findViewById(R.id.okweight);
    okweight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            weightstring = entweight.getText().toString();
            //tester.setText(weightstring);

                Intent intent = new Intent();
                intent.putExtra
                        ("weighttext", weightstring);
                setResult(RESULT_OK, intent);
                if (intent.hasExtra("weighttext")) {
                    finish();
                }


        }
    });
    }
    }

【问题讨论】:

  • 使用 Logcat 检查与崩溃相关的堆栈跟踪:stackoverflow.com/questions/23353173/… 如果您不了解堆栈跟踪,请编辑您的问题并包含堆栈跟踪。
  • 通过尝试使用调试器,您走在了正确的轨道上。你能告诉我们活动在哪一行崩溃吗?

标签: android pointers nullpointerexception null startactivityforresult


【解决方案1】:

错误在这里getIntent();

由于您使用的是onActivityResult ( int requstCode, int resultCode, Intent intent) 方法,您可以在变量intent 中找到您的附加值。方法getIntent() 返回用于启动活动的意图。在您的情况下,它是null

用途:

we = intent.getStringExtra("weighttext");

而不是

we = getIntent().getStringExtra("weighttext");

更好:

        Bundle extras = intent.getExtras();
        if (extras != null) {
           String result = extras.getString("weighttext");
           .....
        }

【讨论】:

  • 谢谢,这是有道理的,我试过了,它奏效了。令人惊讶的是,它实际工作并且在崩溃数周后没有崩溃。
  • 嗨@percy,如果这个或任何答案已经解决了您的问题,请通过单击复选标记考虑accepting。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
【解决方案2】:

在您的接收代码中试试这个

Intent intent= getIntent();
Bundle b =  intent.getExtras();
if(b != null)
we = b.getString("weighttext");

【讨论】:

  • 错了。在这种情况下,getIntent() 为空。他必须在 onActivityResult 方法中使用变量 intent。
【解决方案3】:

您正在调用getIntent() 以获取结果数据。 getIntent() 是一个活动方法,它返回用于启动活动的意图。您应该使用在onActivityResult 方法上传递的intent 变量。

if (requstCode == rcode && resultCode == RESULT_OK) { we = intent.getStringExtra("weighttext"); w = Double.parseDouble(we); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多