Impossible de transmettre des données d'un fragment à un autre à l'aide de l'intention. La logique est bonne .Veuillez vérifier les dernières parties de l'activité principale où j'ai envoyé des chaînes à une autre activité en utilisant putExtra.
Voici le MainActivity.Java:
public class MainActivity extends Activity implements View.OnClickListener {
EditText ed1,ed2,ed3,ed4,ed5,ed6;
Button bt1;
TextView tv1,tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
ed4=(EditText)findViewById(R.id.editText4);
ed5=(EditText)findViewById(R.id.editText5);
ed6=(EditText)findViewById(R.id.editText6);
tv1=(TextView)findViewById(R.id.textView);
tv2=(TextView)findViewById(R.id.textView2);
bt1=(Button)findViewById(R.id.button);
bt1.setOnClickListener(this);
}
public void onClick(View v)
{
String str1=ed1.getText().toString();
String str2=ed2.getText().toString();
String str3=ed3.getText().toString();
String str4=ed4.getText().toString();
String str5=ed5.getText().toString();
String str6=ed6.getText().toString();
if(str1.matches("") || str2.matches("") || str3.matches(""))
{
tv1.setText("One or more fields missing");
if(str4.matches("") || str5.matches("") || str6.matches(""))
{
tv2.setText("One or more fields missing");
}
}
else {
int age1 = Integer.parseInt(ed2.getText().toString());
int age2 = Integer.parseInt(ed5.getText().toString());
int l1 = str1.length();
str1 = str1.toLowerCase();
str4 = str4.toLowerCase();
int l2 = str4.length();
int[] a = new int[26];
int[] b = new int[26];
for (int i = 0; i < l1; i++) {
char c = str1.charAt(i);
int x = (int) c - 97;
a[x]++;
}
for (int i = 0; i < l2; i++) {
char c = str4.charAt(i);
int x = (int) c - 97;
b[x]++;
}
int s=0,p;
for(int i=0;i<26;i++)
{
if(a[i]>b[i])
p=a[i];
else
p=b[i];
s+=p;
}
if(l1>l2)
p=l1;
else
p=l2;
double love1=(double)(s/p)*100;
double love2=0.0,x;
if(age1>age2) {
p = age1;
x=age2;
}
else {
p = age2;
x=age1;
}
love2=(double)(x/p)*100;
Intent in=new Intent(MainActivity.this,SecondActivity.class);
String s1=""+love1;
String s2=""+love2;
in.putExtra("k1",str4);
in.putExtra("k2",str4);
in.putExtra("k3", love1);
in.putExtra("k4",love2);
startActivity(in);
finish();
}
}
}
Voici le code de SecondActivity.Java:
public class SecondActivity extends Activity {
TextView tv7,tv8,tv9,tv10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent in = getIntent();
String s1 = in.getStringExtra("k1");
String s2 = in.getStringExtra("k2");
String s3 = in.getStringExtra("k3");
String s4 = in.getStringExtra("k4");
tv7=(TextView)findViewById(R.id.textView3);
tv8=(TextView)findViewById(R.id.editText4);
tv9=(TextView)findViewById(R.id.editText5);
tv10=(TextView)findViewById(R.id.editText6);
tv7.setText("Hello "+s1);
tv8.setText("Your partner "+s2+" is really Nice");
tv9.setText("Your love percent based on name is "+s3);
tv10.setText("Your love percent based on age is "+s4);
}
}
Voici l'erreur que j'obtiens:
Process: khurana.nikhil.lovemeter, PID: 4019
Java.lang.RuntimeException: Unable to start activity ComponentInfo{khurana.nikhil.lovemeter/khurana.nikhil.lovemeter.SecondActivity}: Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.widget.TextView.setText(Java.lang.CharSequence)' on a null object reference
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2325)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2390)
at Android.app.ActivityThread.access$800(ActivityThread.Java:151)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1303)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5257)
at Java.lang.reflect.Method.invoke(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:903)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:698)
Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.widget.TextView.setText(Java.lang.CharSequence)' on a null object reference
at khurana.nikhil.lovemeter.SecondActivity.onCreate(SecondActivity.Java:32)
at Android.app.Activity.performCreate(Activity.Java:5990)
at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1106)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2278)
Voici activity_second.XML:
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools" Android:layout_width="match_parent"
Android:layout_height="match_parent" Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:background="#cefcfd"
tools:context="khurana.nikhil.lovemeter.SecondActivity">
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text=""
Android:id="@+id/textView3"
Android:layout_alignParentTop="true"
Android:layout_centerHorizontal="true"
Android:layout_marginTop="68dp" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text=""
Android:id="@+id/textView4"
Android:layout_below="@+id/textView3"
Android:layout_centerHorizontal="true"
Android:layout_marginTop="51dp" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text=""
Android:id="@+id/textView5"
Android:layout_above="@+id/textView6"
Android:layout_alignStart="@+id/textView6"
Android:layout_marginBottom="84dp" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text=""
Android:id="@+id/textView6"
Android:layout_alignParentBottom="true"
Android:layout_centerHorizontal="true"
Android:layout_marginBottom="51dp" />
</RelativeLayout>
tv8=(TextView)findViewById(R.id.editText4);
tv9=(TextView)findViewById(R.id.editText5);
tv10=(TextView)findViewById(R.id.editText6);
Remplacez-les par:
tv8=(TextView)findViewById(R.id.textView4);
tv9=(TextView)findViewById(R.id.textView5);
tv10=(TextView)findViewById(R.id.textView6);
Vous venez d'utiliser les mauvais identifiants.