在cardview中使用putstringextra并在意图中获得额外的字符串

编程入门 行业动态 更新时间:2024-10-24 08:25:31
本文介绍了在cardview中使用putstringextra并在意图中获得额外的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

关于这款应用,我已经有3个星期这个问题。让我尽可能简短。 我有5个活动,ActivityA,ActivityB,ActivityC,ActivityD和ActivityE。 ActivityA包含我的`CardView`s(5张卡),我在ActivityA中添加了一个字符串。下面是我的代码片段,关于ActivityA中每张卡片的字符串额外值如何在调用时表现出来。我最初在我的`onBindViewHolder`中的ActivityA中设置了一个最终整数(查询字符串),以便我可以调用每张卡; TheViewHolder.imagee.setOnClickListener(new View.OnClickListener (){b / b @Override public void onClick(查看v){ if(queryString == R.drawable.cardviewone){ 意图intenting = new Intent(getApplicationContext(),ActivityB.class); String one =yesone; intenting.putExtra( Fiveold,one); startActivity(intenting); } else if(queryString == R.drawable.cardviewtwo){ Intent intenting = new Intent(getApplicationContext(),ActivityB.class); String two =yestwo; intenting.putExtra(Fivenew,two); startActivity(intenting); } else if(queryString == R.drawable.cardviewdthree){ 意图打算= new Intent(getApplicationContext(),ActivityB.class); String three =yesthree; intenting.putExtra(Seven,three); startActivity(intenting); } else if(queryString == R.drawable.cardviewfour){ Intent intenting = new Intent(getApplicationContext(), ActivityB.class); 字符串四=yesfour; intenting.putExtra(七,四); startActivity(意图) }; } else if(queryString == R.drawable.cardviewfive){ Intent intenting = new Intent(getApplicationContext(),ActivityB.class); String five =yesfive; intenting.putExtra(America,five); startActivity(intenting); } else { Toast.makeText(getBaseContext(),Application Runtime,Toast.LENGTH_SHORT)。show(); } } }); 上面的代码运行良好。现在到ActivityB ActivityB由2个`TextView`和一个`Button`组成。 以下是我的片段 在我的'onCreate`方法中 Intent intent = getIntent(); String sms = intent.getStringExtra(Fiveold)。toString(); String smss = intent.getStringExtra(Fivenew)。toString(); 虽然两者都是我的完整代码中的字段变量。现在到`Button`的`setOnClickListener`。 if(sms.equals(yesone)){ Log.d(自述文件 ,你的消息是+ sms; if(typedText.getText()。equals(Talk)){ Intent intent = new Intent(ActivityB) .this,ActivityC.class); startActivity(intent); } else if(typedText.getText()。equals(Write)){ 意图intent = new Intent(ActivityB.this,ActivityD.class); startActivity(意图) } } else if(smss.equals(yestwo)){ Intent intent = new Intent(ActivityB.this,ActivityE.class); startActivity(意图); } 对不起,这花了太长时间,现在这个代码运行良好没有 String smss = intent.getStringExtra(Fivenew)。toString(); 当我插入String smss时在else if语句中,代码失败并指向那条线!我尝试将ActivityA中的`getApplicationContext()`更改为ActivityA.this,但它显示错误,所以我无法继续从ActivityA中的其他卡中获取字符串! 如果你能提供帮助就会很高兴。 我的logcat在该行读取nullpointexception代码失败

I've been having this problem for 3 weeks now concerning this app. Let me be as brief as possible. I have 5 activities, ActivityA, ActivityB, ActivityC, ActivityD and ActivityE. ActivityA contains my `CardView`s (5 cards), I put a String extra in ActivityA. Below is a snippet of my code on how my String extra for each cards in ActivityA should behave when they are called. I have initially set a final integer(querystring) in ActivityA in my `onBindViewHolder` to enable me call each cards; TheViewHolder.imagee.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (queryString == R.drawable.cardviewone) { Intent intenting = new Intent(getApplicationContext(), ActivityB.class); String one = "yesone"; intenting.putExtra("Fiveold", one); startActivity(intenting); } else if (queryString == R.drawable.cardviewtwo) { Intent intenting = new Intent(getApplicationContext(), ActivityB.class); String two = "yestwo"; intenting.putExtra("Fivenew", two); startActivity(intenting); } else if (queryString == R.drawable.cardviewdthree) { Intent intenting = new Intent(getApplicationContext(), ActivityB.class); String three = "yesthree"; intenting.putExtra("Seven", three); startActivity(intenting); } else if (queryString == R.drawable.cardviewfour) { Intent intenting = new Intent(getApplicationContext(), ActivityB.class); String four = "yesfour"; intenting.putExtra("Seven", four); startActivity(intenting); } else if (queryString == R.drawable.cardviewfive) { Intent intenting = new Intent(getApplicationContext(), ActivityB.class); String five = "yesfive"; intenting.putExtra("America", five); startActivity(intenting); } else { Toast.makeText(getBaseContext(), "Application Runtime", Toast.LENGTH_SHORT).show(); } } }); The code above is working well. Now to ActivityB ActivityB is made up of 2 `TextView`s and a `Button`. below is my snippet In my `onCreate` method here Intent intent = getIntent(); String sms = intent.getStringExtra("Fiveold").toString(); String smss =intent.getStringExtra("Fivenew").toString(); Though both are field variables in my full code. Now to the `setOnClickListener` of the `Button`. if (sms.equals("yesone")) { Log.d("Readme", "Your message is " + sms); if (typedText.getText().equals("Talk")) { Intent intent = new Intent(ActivityB.this, ActivityC.class); startActivity(intent); } else if (typedText.getText().equals("Write")) { Intent intent = new Intent(ActivityB.this, ActivityD.class); startActivity(intent) } } else if (smss.equals("yestwo")) { Intent intent = new Intent(ActivityB.this, ActivityE.class); startActivity(intent); } Sorry this is taking too long, Now this code works well without the String smss =intent.getStringExtra("Fivenew").toString(); When I insert the String smss in the else if statement, the code fails and points to that line! I tried changing my `getApplicationContext()` in ActivityA to ActivityA.this but it was showing an error so I couldn't proceed with getting the Strings from the other cards in ActivityA! WOULD BE GLAD IF YOU CAN HELP. My logcat reads nullpointexception on that line the code fails

推荐答案

在你的活动A中,你有这个 In your Activity A, you have this @Override public void onClick(View v) { if (queryString == R.drawable.cardviewone) { Intent intenting = new Intent(getApplicationContext(), ActivityB.class); String one = "yesone"; intenting.putExtra("Fiveold", one); startActivity(intenting); } else if (queryString == R.drawable.cardviewtwo) { Intent intenting = new Intent(getApplicationContext(), ActivityB.class); String two = "yestwo"; intenting.putExtra("Fivenew", two); startActivity(intenting); } else if{...}

你正在使用if-else条件。如果其中一个条件匹配,调试器将运行到活动B.假设它匹配

You are using if-else condition. Debugger will run to Activity B if one of the condition is matched. Assuming it matches with the

if (queryString == R.drawable.cardviewone)

,它只会通过Fiveold到Activity B,因此你将在这行中得到nullPointerExeption

, it will only pass "Fiveold" to Activity B and hence you will get nullPointerExeption in this line

String smss =intent.getStringExtra("Fivenew").toString();

因为它不会运行另一个条件。 我希望你能清楚。

since it is not going to run another condition. I hope it is clear for you.

更多推荐

在cardview中使用putstringextra并在意图中获得额外的字符串

本文发布于:2023-11-28 07:46:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   图中   在意   cardview   putstringextra

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!