Reputation: 209
i have to activities, the first activity contains an EditText & a button, when i press the button the value entered in the EditText field should be transferred to a TextView in the other activity, but when i run the app, enter some text & click the button, the app crashes & the emulator shows an error, i tried a lot of things, but didn't know what's the problem
FIRST ACTIVITY CODE:
package com.example.sqa;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class TheFirst extends Activity {
TextView tv1;
EditText ed1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_first);
tv1=(TextView)findViewById(R.id.textView1);
ed1=(EditText)findViewById(R.id.editText1);
b1=(Button)findViewById(R.id.button1);
final Intent i1=new Intent(TheFirst.this,TheSecond.class);
String temp=ed1.getText().toString();
i1.putExtra("v1", temp);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(i1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.the_first, menu);
return true;
}
}
SECONDE ACTIVITY CODE:
package com.example.sqa;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TheSecond extends Activity {
TextView tv2;
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_second);
tv2=(TextView)findViewById(R.id.textView2);
b2=(Button)findViewById(R.id.button2);
Intent i2=getIntent();
tv2.setText(i2.getStringExtra("v1"));
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i1=new Intent(TheSecond.this,TheFirst.class);
startActivity(i1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.the_second, menu);
return true;
}
}
AndroidManifest.xml FILE:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sqa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sqa.TheFirst"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.sqa.TheSecond"
android:label="@string/title_activity_the_second" >
</activity>
</application>
</manifest>
LOGCAT:
04-10 19:12:31.070: D/dalvikvm(1714): GC_FOR_ALLOC freed 89K, 5% free 3279K/3440K, paused 45ms, total 48ms
04-10 19:12:31.130: D/gralloc_goldfish(1714): Emulator without GPU emulation detected.
04-10 19:12:37.530: D/AndroidRuntime(1714): Shutting down VM
04-10 19:12:37.530: W/dalvikvm(1714): threadid=1: thread exiting with uncaught exception (group=0xb1afdb90)
04-10 19:12:37.580: E/AndroidRuntime(1714): FATAL EXCEPTION: main
04-10 19:12:37.580: E/AndroidRuntime(1714): Process: com.example.sqa, PID: 1714
04-10 19:12:37.580: E/AndroidRuntime(1714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqa/com.example.sqa.TheSecond}: java.lang.NullPointerException
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.os.Handler.dispatchMessage(Handler.java:102)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.os.Looper.loop(Looper.java:137)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-10 19:12:37.580: E/AndroidRuntime(1714): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 19:12:37.580: E/AndroidRuntime(1714): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 19:12:37.580: E/AndroidRuntime(1714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-10 19:12:37.580: E/AndroidRuntime(1714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-10 19:12:37.580: E/AndroidRuntime(1714): at dalvik.system.NativeStart.main(Native Method)
04-10 19:12:37.580: E/AndroidRuntime(1714): Caused by: java.lang.NullPointerException
04-10 19:12:37.580: E/AndroidRuntime(1714): at com.example.sqa.TheSecond.onCreate(TheSecond.java:26)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.Activity.performCreate(Activity.java:5243)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-10 19:12:37.580: E/AndroidRuntime(1714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-10 19:12:37.580: E/AndroidRuntime(1714): ... 11 more
04-10 19:13:03.340: I/Process(1714): Sending signal. PID: 1714 SIG: 9
activity_the_first.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TheFirst" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="@string/s2" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/s3" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="@string/s1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
activity_the_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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TheSecond" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/s4"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="@string/s3" />
</RelativeLayout>
Upvotes: 0
Views: 286
Reputation: 209
Well i think everything is clear now, conflicting IDs i didn't pay attention for that because i use Eclipse & i don't mess with default IDs usually, & this is the first time i deal with multi-activities, so it seems like eclipse give different IDs to object in one layout, & once i create another activity layout it gives the same IDs again, although both layouts are in the same project. Thank You All Guys.
Upvotes: 0
Reputation: 435
In your second activity replace this line. tv2.setText(i2.getStringExtra("v1")); to String text = getIntent().getExtras().getString("vi"); tv2.setText(text);
Upvotes: 0
Reputation: 9904
Are you sure the below is correct?
b2=(Button)findViewById(R.id.button1);
R.id.button1 or something else?
Upvotes: 0
Reputation: 47817
First Move this
String temp=ed1.getText().toString();
Inside your Button onclick()
event like:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String temp=ed1.getText().toString();
i1.putExtra("v1", temp);
startActivity(i1);
}
});
Upvotes: 4