我正在尝试将字符串从活动传递到另一个。我经历了许多提到“意图”和“传递字符串”的问题,但我仍然遇到同样的错误。有人能指出我在哪里弄错了吗?这是我的代码
第一项活动
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);
}
}
第二项活动
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);
}
}
清单
<?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)
我尝试了许多形式的Intent,并且在我的代码中的不同位置,但是当我的第二个活动启动时,我的应用程序在按“Ok”时仍然崩溃。
感谢您的帮助!!
答案 0 :(得分:1)
在第一个活动中评论此部分并执行代码,如果您遇到相同的错误,则可能未在清单中注册“com.example.youtube.domotique”和“com.example.youtube.LoginIn” / p>
if (IPAddress != ""){
startActivity(new Intent("com.example.youtube.LoginIn"));
}