关于这款应用,我已经有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并在意图中获得额外的字符串
发布评论