如何将数据从一个 xml 发送到另一个 xml 布局?

发布于 2024-12-17 20:45:52 字数 15391 浏览 0 评论 0原文

1.第一个活动: 此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 检索数据。我需要

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); }
    } }

在此处向第二个 xml 文件 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>

1.First Activity :
This Activity has two EditTexts and two Buttons.
I need to give login id and password.Here
the Buttons contained logging in and cancel:

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:
This Activity retrieves the data from the first Activity. I need to show user details to second xml file

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 here:

<?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.needed output can be displayed here:

<?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.this is my Logcat getting errors

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 manifest file:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ㄖ落Θ余辉 2024-12-24 20:45:52

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>

In SameLayoutActivity replace this,

 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);
            }
        });

In your Login Activity, Replace this code, And try again and let me know what happen ..

  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);
            }
    }

Manifest file:

    <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>
眼泪淡了忧伤 2024-12-24 20:45:52

您需要通过用于启动第二个 Activity 的 Intent 传递数据。查看这篇博客文章

You need to pass your data through the Intent used to start the 2nd Activity. Check this blog post out.

最后的乘客 2024-12-24 20:45:52

设置数据绑定来轻松实现这一点。

dataBinding {
    enabled = true
}

您可以通过在 Build.gradle(:app) 内部

现在,要将数据从一个 xml 发送到另一个 xml,请查看下面的示例。在此,我制作了一个名为 item_top_card.xml 的布局,我想将其包含在另一个具有不同标题的 xml 文件中。一种方法是为不同的标题制作不同的 xml。一种好方法是将标题字符串从一个 xml 传递到另一个 xml,如下所示:

<include
   android:id="@+id/todays_clicks_parent"
   app:option="@{@string/todays_clicks}"
   layout="@layout/item_top_card" />

您可以包含要包含在其他 xml 文件中的布局 xml,如上所示。请注意,这里我们在“option”参数中传递字符串。这是参数名称,可以根据您的需要进行更改。以下是 item_top_card.xml 的文件:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable name="option" type="java.lang.String"/>
    </data>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="120dp"
        android:minHeight="120dp"
        android:layout_marginLeft="16dp"
        android:background="@color/transparent"
        app:cardBackgroundColor="@color/white"
        app:cardElevation="0.1dp"
        app:cardCornerRadius="8dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingVertical="12dp"
            android:paddingLeft="12dp"
            android:paddingRight="24dp">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/background_circle"
                android:backgroundTint="@color/purple_light"
                android:paddingHorizontal="12dp"
                android:paddingVertical="12dp"
                android:src="@drawable/clicks"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:lineHeight="24dp"
                android:text="123"
                android:paddingVertical="5dp"
                android:paddingLeft="5dp"
                android:lineSpacingExtra="5sp"
                android:textColor="@color/black"
                android:textFontWeight="600"
                android:textSize="16sp"
                app:layout_constraintStart_toStartOf="@+id/icon"
                app:layout_constraintTop_toBottomOf="@+id/icon" />

            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lineHeight="20dp"
                tools:text="@string/todays_clicks"
                android:text="@{option}"
                android:paddingLeft="5dp"
                android:layout_marginTop="4dp"
                android:textColor="@color/light_grey_text_color"
                android:textSize="14sp"
                android:lineSpacingExtra="3sp"
                app:layout_constraintStart_toStartOf="@+id/title"
                app:layout_constraintTop_toBottomOf="@+id/title" />

        </androidx.constraintlayout.widget.ConstraintLayout>


    </com.google.android.material.card.MaterialCardView>
</layout>

You can achieve this easily by data binding by setting

dataBinding {
    enabled = true
}

inside your Build.gradle(:app)

Now, to send data from one xml to another, look at the below example. In this, I have made a layout named item_top_card.xml which I want to include it in another xml file with different titles. One way is to make different xml for different titles. A good way is to pass the title string from one xml to another as shown :

<include
   android:id="@+id/todays_clicks_parent"
   app:option="@{@string/todays_clicks}"
   layout="@layout/item_top_card" />

You can include the layout xml which you want to include in other xml file as shown above. Notice that here we are passing the string in "option" parameter. This is the parameter name which can be changed as per your needs. Below is the file for item_top_card.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable name="option" type="java.lang.String"/>
    </data>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="120dp"
        android:minHeight="120dp"
        android:layout_marginLeft="16dp"
        android:background="@color/transparent"
        app:cardBackgroundColor="@color/white"
        app:cardElevation="0.1dp"
        app:cardCornerRadius="8dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingVertical="12dp"
            android:paddingLeft="12dp"
            android:paddingRight="24dp">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/background_circle"
                android:backgroundTint="@color/purple_light"
                android:paddingHorizontal="12dp"
                android:paddingVertical="12dp"
                android:src="@drawable/clicks"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:lineHeight="24dp"
                android:text="123"
                android:paddingVertical="5dp"
                android:paddingLeft="5dp"
                android:lineSpacingExtra="5sp"
                android:textColor="@color/black"
                android:textFontWeight="600"
                android:textSize="16sp"
                app:layout_constraintStart_toStartOf="@+id/icon"
                app:layout_constraintTop_toBottomOf="@+id/icon" />

            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lineHeight="20dp"
                tools:text="@string/todays_clicks"
                android:text="@{option}"
                android:paddingLeft="5dp"
                android:layout_marginTop="4dp"
                android:textColor="@color/light_grey_text_color"
                android:textSize="14sp"
                android:lineSpacingExtra="3sp"
                app:layout_constraintStart_toStartOf="@+id/title"
                app:layout_constraintTop_toBottomOf="@+id/title" />

        </androidx.constraintlayout.widget.ConstraintLayout>


    </com.google.android.material.card.MaterialCardView>
</layout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文