I’m trying to pass a string from an activity to another. I went through many of the questions refering to “Intents” and “Passing strings” but I keep getting the same error. Could someone point me out where I’m mistaken? Here’s my code
First Activity
package com.example.youtube;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class Testing extends Activity {
public static String IPAddress = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
//Alert Popup
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Step 1");
alert.setMessage("Enter IP Address\n(i.e: 192.168.0.1)");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Do something with value!
IPAddress = value.toString();
//Fire that second activity
Intent intent = new Intent( getBaseContext(),LoginIn.class);
intent.putExtra("keyword1",IPAddress);
startActivity( intent);
if (IPAddress != ""){
startActivity(new Intent("com.example.youtube.LoginIn"));
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
finish();
System.exit(0);
}
});
alert.show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
}
Second Activity
package com.example.youtube;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
public class LoginIn extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button bverify = (Button) findViewById(R.id.verify1);
final TextView testview = (TextView) findViewById(R.id.textView3);
Bundle extras=getIntent().getExtras();
String value1=extras.getString("keyword1");
bverify.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.example.youtube.domotique"));
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.youtube"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Testing"
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=".LoginIn"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.youtube.LoginIn" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".domotique"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.youtube.domotique" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat
threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
FATAL EXCEPTION: main
Unable to start activity ComponentInfo{com.example.youtube/com.example.youtube.LoginIn}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.youtube.LoginIn.onCreate(LoginIn.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
I tried many form of Intent, and at different location in my code but my App keeps crashing when pressing “Ok”, when my Second activity is launched.
Thanks for any help!!
Comment this section in your first activity and execute the code, if you got the same error then you might not be registered “com.example.youtube.domotique” and “com.example.youtube.LoginIn” in your manifest