Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
wseng 26-Dec-15 5:03am    
can you format your code nicely? It's hard to read.

1 solution

In your Activity A, you have this

Java
@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{...}


You are using if-else condition. Debugger will run to Activity B if one of the condition is matched. Assuming it matches with the
Java
if (queryString == R.drawable.cardviewone)
, it will only pass "Fiveold" to Activity B and hence you will get nullPointerExeption in this line
Java
String smss =intent.getStringExtra("Fivenew").toString();
since it is not going to run another condition.

I hope it is clear for you.
 
Share this answer
 
v2
Comments
Richard MacCutchan 26-Dec-15 7:12am    
It would help if you also formatted your code with proper indentation, and newlines.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900