User onactivityresult 中的resultcode返回0 怎么处理

时间: 2024-04-06 19:34:56 浏览: 9
如果 `onActivityResult` 中的 `resultCode` 返回 0,表示操作没有成功,需要根据实际情况进行相应的处理。一般来说,我们可以通过以下方式来处理: 1. 检查请求是否被正确处理:检查发送请求的页面是否正确实现了请求处理逻辑,以及请求处理逻辑是否正确。 2. 检查请求参数是否正确:检查发送请求的页面是否正确地设置了请求参数,例如是否设置了必要的参数、参数是否符合要求等。 3. 检查网络连接是否正常:如果请求需要网络连接,需要检查网络连接是否正常,例如检查网络是否可用、网络连接是否稳定等。 4. 给出相应的提示信息:如果无法确定错误原因,可以给用户一个提示信息,告诉用户操作失败的原因,并提供相应的解决方案。 总之,当 `onActivityResult` 中的 `resultCode` 返回 0 时,需要根据具体情况进行相应的处理,找出错误原因并进行解决。
相关问题

@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent){ super.onActivityResult(requestCode,resultCode,intent); if(intent==null){ return; } switch (requestCode){ case REQUEST_REGISTER_CODE: if (resultCode==11){ tvUserId.setText(intent.getStringExtra("userId")); tvUsername.setText(intent.getStringExtra("userName")); ivLogin.setImageResource(R.mipmap.user_2); Toast.makeText(this,"注册成功!",Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this,"注册失败!",Toast.LENGTH_LONG).show(); } break; case REQUEST_LOGIN_CODE: if(resultCode==21){ tvUserId.setText(intent.getStringExtra("userId")); tvUsername.setText(intent.getStringExtra("userName")); ivLogin.setImageResource(R.mipmap.user_2); Toast.makeText(this,"登录成功!",Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this,"登录失败!",Toast.LENGTH_LONG).show(); } break; default: Toast.makeText(this,"操作失败!",Toast.LENGTH_LONG).show(); break; } }现在有一个新的页面Activity应如何修改本代码进行跳转

如果你要在当前Activity中跳转到一个新的Activity,你需要在当前Activity中添加一个Intent,然后调用startActivity方法来启动新的Activity。以下是一个示例代码: ``` Intent intent = new Intent(CurrentActivity.this, NewActivity.class); startActivity(intent); ``` 在上面的代码中,CurrentActivity是当前Activity的类名,而NewActivity是你要跳转的Activity的类名。如果你需要传递一些参数给新的Activity,可以使用Intent的putExtra方法来实现。例如: ``` Intent intent = new Intent(CurrentActivity.this, NewActivity.class); intent.putExtra("userId", userId); intent.putExtra("userName", userName); startActivity(intent); ``` 在上面的代码中,我们使用了putExtra方法来传递了两个参数"userId"和"userName"给新的Activity。在新的Activity中,你可以使用getIntent方法来获取这些参数。例如: ``` Intent intent = getIntent(); String userId = intent.getStringExtra("userId"); String userName = intent.getStringExtra("userName"); ```

假设我们要开发一个电商app,匿名用户也可以进入首页浏览商品,如果需要购买商品,则必须登录后才能购买,登录后将当前登录用户账号返回到首页并保存到首页中,然后自动把该商品加入到购物车页面。要求登录页面在该app中使用单例模式。首页只需要做一个商品列表,每个商品都有图片、名称、价格和一个购买按钮;登录页面能输入账号、密码;购物车页面能获取传过来的用户账号、商品名称、价格,并用TextView控件显示出来。

以下是一个实现上述功能的简单电商app的代码示例。其中包括了单例模式的登录页面代码。 MainActivity.java ```java import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ArrayList<Product> products = new ArrayList<>(); private String currentUser = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化产品列表 initProducts(); // 设置产品列表的适配器 ProductAdapter adapter = new ProductAdapter(this, products); ListView listView = findViewById(R.id.list_view); listView.setAdapter(adapter); // 设置产品列表的点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { if (currentUser == null) { // 如果用户未登录,则跳转到登录页面 Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivityForResult(intent, 1); } else { // 如果用户已登录,则将该产品添加到购物车页面 Cart.getInstance().addProduct(currentUser, products.get(position)); } } }); } // 初始化产品列表 private void initProducts() { products.add(new Product("商品1", "这是商品1的描述", 50, R.drawable.product1)); products.add(new Product("商品2", "这是商品2的描述", 100, R.drawable.product2)); products.add(new Product("商品3", "这是商品3的描述", 150, R.drawable.product3)); products.add(new Product("商品4", "这是商品4的描述", 200, R.drawable.product4)); products.add(new Product("商品5", "这是商品5的描述", 250, R.drawable.product5)); products.add(new Product("商品6", "这是商品6的描述", 300, R.drawable.product6)); } // 处理登录页面返回的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == RESULT_OK) { // 获取登录页面返回的用户账号 currentUser = data.getStringExtra("username"); // 更新首页的UI setTitle("欢迎," + currentUser); } } } ``` ProductAdapter.java ```java import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; public class ProductAdapter extends BaseAdapter { private Context context; private ArrayList<Product> products; public ProductAdapter(Context context, ArrayList<Product> products) { this.context = context; this.products = products; } @Override public int getCount() { return products.size(); } @Override public Object getItem(int position) { return products.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); } ImageView imageView = convertView.findViewById(R.id.image_view); TextView nameTextView = convertView.findViewById(R.id.name_text_view); TextView priceTextView = convertView.findViewById(R.id.price_text_view); Product product = products.get(position); imageView.setImageResource(product.getImageResourceId()); nameTextView.setText(product.getName()); priceTextView.setText(String.valueOf(product.getPrice())); return convertView; } } ``` LoginActivity.java ```java import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; public class LoginActivity extends AppCompatActivity { private EditText usernameEditText; private EditText passwordEditText; private static LoginActivity instance = null; public static LoginActivity getInstance() { return instance; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // 将当前活动设置为单例实例 instance = this; usernameEditText = findViewById(R.id.username_edit_text); passwordEditText = findViewById(R.id.password_edit_text); Button loginButton = findViewById(R.id.login_button); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = usernameEditText.getText().toString(); String password = passwordEditText.getText().toString(); if (isValid(username, password)) { // 如果用户名和密码都正确,则返回结果给调用者 Intent intent = new Intent(); intent.putExtra("username", username); setResult(RESULT_OK, intent); finish(); } else { // 如果用户名或密码不正确,则弹出提示框 Context context = LoginActivity.this; String text = "用户名或密码不正确,请重试!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); } } }); } // 判断用户名和密码是否正确 private boolean isValid(String username, String password) { return "admin".equals(username) && "123456".equals(password); } } ``` Cart.java ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Cart { private Map<String, List<Product>> cartMap = new HashMap<>(); private static Cart instance = null; public static Cart getInstance() { if (instance == null) { instance = new Cart(); } return instance; } // 将指定用户的指定产品添加到购物车中 public void addProduct(String username, Product product) { List<Product> productList = cartMap.get(username); if (productList == null) { productList = new ArrayList<>(); cartMap.put(username, productList); } productList.add(product); } // 获取指定用户的购物车中的所有产品列表 public List<Product> getProducts(String username) { List<Product> productList = cartMap.get(username); if (productList == null) { productList = new ArrayList<>(); } return productList; } } ``` CartActivity.java ```java import android.content.Intent; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.List; public class CartActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cart); // 获取传递过来的用户账号 Intent intent = getIntent(); String username = intent.getStringExtra("username"); // 获取该用户的购物车中的所有产品列表 List<Product> products = Cart.getInstance().getProducts(username); // 将产品列表显示在界面上 LinearLayout productLayout = findViewById(R.id.product_layout); for (Product product : products) { LinearLayout itemLayout = new LinearLayout(this); itemLayout.setOrientation(LinearLayout.HORIZONTAL); TextView nameTextView = new TextView(this); nameTextView.setText(product.getName() + ":"); nameTextView.setTextSize(16); TextView priceTextView = new TextView(this); priceTextView.setText(String.valueOf(product.getPrice()) + "元"); priceTextView.setTextSize(16); itemLayout.addView(nameTextView); itemLayout.addView(priceTextView); productLayout.addView(itemLayout); } } } ``` activity_main.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> ``` list_item.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <ImageView android:id="@+id/image_view" android:layout_width="96dp" android:layout_height="96dp"/> <LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:paddingLeft="16dp"> <TextView android:id="@+id/name_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold"/> <TextView android:id="@+id/price_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp"/> </LinearLayout> <Button android:id="@+id/buy_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="购买"/> </LinearLayout> ``` activity_login.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <EditText android:id="@+id/username_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> <EditText android:id="@+id/password_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword"/> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout> ``` activity_cart.xml ```xml <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas

相关推荐

最新推荐

recommend-type

Android onActivityResult和setResult方法详解及使用

主要介绍了Android onActivityResult和setResult方法详解及使用的相关资料,这里提供实例,帮助大家学习理解,需要的朋友可以参考下
recommend-type

Android中onActivityResult的用法

共享这份Android中onActivityResult的用法给大家,希望对大家有帮助。
recommend-type

仿Haier 海尔家电家居触屏版html5响应式手机wap企业网站模板.zip

触屏版自适应手机wap软件网站模板 触屏版自适应手机wap软件网站模板
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

numpy数组索引与切片技巧

![numpy数组索引与切片技巧](https://img-blog.csdnimg.cn/f610d87ed50745d2b7052af887da2d0d.png) # 2.1 整数索引 整数索引是 NumPy 数组中索引元素的最简单方法。它允许您使用整数来访问数组中的特定元素或子数组。 ### 2.1.1 单个元素索引 单个元素索引使用一个整数来访问数组中的单个元素。语法为: ```python array[index] ``` 其中: * `array` 是要索引的 NumPy 数组。 * `index` 是要访问的元素的索引。 例如: ```python import
recommend-type

javaboolean类型怎么使用

Java中的boolean类型表示真或假,只有两个可能的值。在Java中,boolean类型的变量可以被初始化为false或true。可以使用以下语法来声明和初始化一个boolean类型的变量: ``` boolean myBoolean = true; ``` 在Java中,boolean类型的变量通常用于控制流程和条件测试,例如: ``` if (myBoolean) { // do something if myBoolean is true } else { // do something if myBoolean is false } ``` 除了if语句之外
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

Selenium与人工智能结合:图像识别自动化测试

![Selenium与人工智能结合:图像识别自动化测试](https://img-blog.csdnimg.cn/8a58f7ef02994d2a8c44b946ab2531bf.png) # 1. Selenium简介** Selenium是一个用于Web应用程序自动化的开源测试框架。它支持多种编程语言,包括Java、Python、C#和Ruby。Selenium通过模拟用户交互来工作,例如单击按钮、输入文本和验证元素的存在。 Selenium提供了一系列功能,包括: * **浏览器支持:**支持所有主要浏览器,包括Chrome、Firefox、Edge和Safari。 * **语言绑