gpt4 book ai didi

安卓 startActivityForResult()

转载 作者:行者123 更新时间:2023-11-30 02:12:56 27 4
gpt4 key购买 nike

我刚开始学习 android 编程,但我在启动 Activity for Result(使用 onActivityResult()、setResult())时遇到问题。

首先,有三个 Activity - Main、Register、SubActivity(但我的问题是关于两个 Activity - Main、Register)。我像这样将它们全部注册到“AndroidManifest.xml”:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wakwakwak.iwak.myapp" >
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SubActivity"
android:label="Here is SubWorld!">

</activity>
<activity android:name=".Register"
android:label="Register">

</activity>
</application>

</manifest>

如您所见,三个 Activity 已成功注册。

这是主要 Activity 。

Main Activity

如果我触摸“注册”按钮,我们将转到“注册” Activity 。

Register Activity

如果 REG 被触摸,Register Activity 将信息返回给 Main Activity,Main Activity 将其保存到 ArrayList。

下面的代码是Main Activity(Main.java)。

package com.wakwakwak.iwak.myapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;


public class Main extends Activity {
private static ArrayList<Person> users;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(300);
}

public void onClick(View v){
EditText edId = (EditText)findViewById(R.id.edit_id);
EditText edPw = (EditText)findViewById(R.id.edit_pw);
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
switch(v.getId()){
case R.id.btn_login:
vibrator.vibrate(300);
Person person = new Person(edId.getText().toString(),edPw.getText().toString());

Intent intent1 = new Intent(this, SubActivity.class);
intent1.putExtra("person",person);
startActivity(intent1);
break;
case R.id.btn_register:
vibrator.vibrate(300);
Intent intent2 = new Intent(this,Register.class);
startActivityForResult(intent2, 0);
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case 0:
Person person = (Person)data.getSerializableExtra("person");
users.add(person);
Toast.makeText(this,person.toString(),Toast.LENGTH_SHORT).show();
break;
}
}
}

下面的代码是Register Activity(Register.java)

package com.wakwakwak.iwak.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}

public void onClick(View view) {
EditText editId = (EditText)findViewById(R.id.edit_id);
EditText editPw = (EditText)findViewById(R.id.edit_pw);
EditText editPwAgain = (EditText)findViewById(R.id.edit_pwagain);
switch(view.getId()){
case R.id.btn_register:
if(!editPw.getText().toString().equals(editPwAgain.getText().toString())){
editPw.setText("");
editPwAgain.setText("");
Toast.makeText(this,"Passwords are not equal each other",Toast.LENGTH_SHORT).show();
}
else{
Person person = new Person(editId.getText().toString(),editPw.getText().toString());
Intent intent2 = new Intent();
intent2.putExtra("person",person);
setResult(0,intent2);
finish();
}
break;
}
}
}

(Person是一个有成员'id'和'pw'的类。Person类似乎没有问题)

Register activity 本身似乎运行良好 - 当每个密码不同时它显示错误提示,但当它们相同时(传递给 Main activity 时),发生错误:'Myapp is stopped' 我真的不知道有什么问题!!请让我知道哪里出了问题:(

最佳答案

when both of them are same(when passing to Main activity), Error occurs: 'Myapp is stopped'

可能得到 NPE异常 users ArrayList 是 null打电话时 add onActivityResult 中的方法.在 onCreate 中添加以下行方法:

users=new ArrayList<Person>();

关于安卓 startActivityForResult(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873109/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com