1.First Activity
:
此Activity
有两个EditTexts
和两个Buttons
。
我需要提供登录ID和密码。这里
Buttons
包含登录并取消:
package com.samelayout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SameLayoutActivity extends Activity {
/** Called when the activity is first created. */
EditText Myusername;
EditText Mypassword;
Button btnlogin;
Button btncancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnlogin=(Button)findViewById(R.id.loginbutton);
btncancel=(Button)findViewById(R.id.cancelbutton);
Myusername=(EditText)findViewById(R.id.username);
Mypassword=(EditText)findViewById(R.id.password);
btnlogin.setOnClickListener(new OnClickListener()
{ public void onClick(View v) {
String str1=Myusername.getText().toString();
String str2=Mypassword.getText().toString();
Intent int1=new Intent(getApplicationContext(),Login.class);
Bundle b= new Bundle();
b.putString("userdata", str1);
b.putString("userdata1", str2);
int1.putExtras(b);
int1.putExtras(b);
//(int1);
startActivityForResult(int1, 0);
}
});
btncancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
2.second Activity
:
此Activity
检索第一个Activity
中的数据。我需要向第二个xml文件显示用户详细信息
package com.samelayout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Login extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//Intent int1= getIntent();
Bundle b=this.getIntent().getExtras();
if(b!=null)
{
String str1=b.getString("userdata");
String str2=b.getString("userdata1");
TextView tv1=(TextView)findViewById(R.id.text1);
TextView tv2=(TextView)findViewById(R.id.text2);
tv1.setText(str1);
tv2.setText(str2);
setContentView(tv1);
setContentView(tv2); }
} }
3. main.xml
在这里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Linearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="@android:color/darker_gray" >
<TextView android:layout_width="fill_parent"
android:text="Employee ID:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20sp"/>
<EditText android:hint="enter ur ID"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/username"/>
<TextView android:layout_width="fill_parent"
android:text="Password:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20dip"/>
<EditText android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:password="true"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/loginbutton"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancelbutton"/>
</LinearLayout>
4.此处可显示需要的输出:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Linearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="@android:color/darker_gray" >
<TextView
android:layout_width="fill_parent"
android:id="@+id/text1"
android:text="Employee Name:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:id="@+id/text2"
android:text="Employee password:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20sp"/>
</LinearLayout>
5.这是我的Logcat收到错误
11-22 12:04:27.298: W/KeyCharacterMap(334): No keyboard for id 0
11-22 12:04:27.298: W/KeyCharacterMap(334): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-22 12:04:37.738: D/AndroidRuntime(334): Shutting down VM
11-22 12:04:37.738: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 12:04:37.758: E/AndroidRuntime(334): FATAL EXCEPTION: main
11-22 12:04:37.758: E/AndroidRuntime(334): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 12:04:37.758: E/AndroidRuntime(334): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:41)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.view.View.performClick(View.java:2485)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.view.View$PerformClick.run(View.java:9080)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.os.Handler.handleCallback(Handler.java:587)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 12:04:37.758: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:04:37.758: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 12:04:37.758: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 12:04:37.758: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 12:04:37.758: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
11-22 12:04:42.079: I/Process(334): Sending signal. PID: 334 SIG: 9
11-22 14:46:39.458: W/KeyCharacterMap(377): No keyboard for id 0
11-22 14:46:39.458: W/KeyCharacterMap(377): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-22 14:46:42.309: D/AndroidRuntime(377): Shutting down VM
11-22 14:46:42.309: W/dalvikvm(377): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 14:46:42.329: E/AndroidRuntime(377): FATAL EXCEPTION: main
11-22 14:46:42.329: E/AndroidRuntime(377): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 14:46:42.329: E/AndroidRuntime(377): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:38)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.view.View.performClick(View.java:2485)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.view.View$PerformClick.run(View.java:9080)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.os.Handler.handleCallback(Handler.java:587)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.os.Looper.loop(Looper.java:123)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 14:46:42.329: E/AndroidRuntime(377): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 14:46:42.329: E/AndroidRuntime(377): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 14:46:42.329: E/AndroidRuntime(377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 14:46:42.329: E/AndroidRuntime(377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 14:46:42.329: E/AndroidRuntime(377): at dalvik.system.NativeStart.main(Native Method)
11-22 14:48:22.648: D/AndroidRuntime(411): Shutting down VM
11-22 14:48:22.648: W/dalvikvm(411): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 14:48:22.657: E/AndroidRuntime(411): FATAL EXCEPTION: main
11-22 14:48:22.657: E/AndroidRuntime(411): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 14:48:22.657: E/AndroidRuntime(411): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:38)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.view.View.performClick(View.java:2485)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.view.View$PerformClick.run(View.java:9080)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.os.Handler.handleCallback(Handler.java:587)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.os.Looper.loop(Looper.java:123)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 14:48:22.657: E/AndroidRuntime(411): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 14:48:22.657: E/AndroidRuntime(411): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 14:48:22.657: E/AndroidRuntime(411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 14:48:22.657: E/AndroidRuntime(411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 14:48:22.657: E/AndroidRuntime(411): at dalvik.system.NativeStart.main(Native Method)
11-22 14:59:44.668: W/KeyCharacterMap(441): No keyboard for id 0
11-22 14:59:44.668: W/KeyCharacterMap(441): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-22 14:59:53.378: D/AndroidRuntime(441): Shutting down VM
11-22 14:59:53.388: W/dalvikvm(441): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 14:59:53.411: E/AndroidRuntime(441): FATAL EXCEPTION: main
11-22 14:59:53.411: E/AndroidRuntime(441): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 14:59:53.411: E/AndroidRuntime(441): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:38)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.view.View.performClick(View.java:2485)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.view.View$PerformClick.run(View.java:9080)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.os.Handler.handleCallback(Handler.java:587)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.os.Looper.loop(Looper.java:123)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 14:59:53.411: E/AndroidRuntime(441): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 14:59:53.411: E/AndroidRuntime(441): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 14:59:53.411: E/AndroidRuntime(441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 14:59:53.411: E/AndroidRuntime(441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 14:59:53.411: E/AndroidRuntime(441): at dalvik.system.NativeStart.main(Native Method)
11-22 15:04:53.512: I/Process(441): Sending signal. PID: 441 SIG: 9
6.android清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samelayout"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name=".SameLayoutActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
在 SameLayoutActivity 中替换此
btnlogin.setOnClickListener(new OnClickListener()
{ public void onClick(View v) {
String str1=Myusername.getText().toString();
String str2=Mypassword.getText().toString();
Intent int1=new Intent(SameLayoutActivity.this,Login.class);
Bundle b= new Bundle();
b.putString("userdata", str1);
b.putString("userdata1", str2);
int1.putExtras(b);
//(int1);
startActivityForResult(int1, 0);
}
});
在登录活动中,替换此代码,然后再试一次,让我知道发生了什么......
TextView tv1=(TextView)findViewById(R.id.text1);
TextView tv2=(TextView)findViewById(R.id.text2);
if(b!=null)
{
String str1=b.getString("userdata");
String str2=b.getString("userdata1");
tv1.setText(str1);
tv2.setText(str2);
}
}
清单文件:
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name=".SameLayoutActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login"> </activity>
</application>
</manifest>
答案 1 :(得分:1)
您需要通过用于启动第二个活动的Intent
传递数据。请查看此blog post。