从一个活动切换到另一个活动时意图不起作用

时间:2013-04-30 06:20:04

标签: android android-intent

我制作了一个简单的android程序,有两个活动,第一个活动包含一个edittext和一个按钮,第二个活动包含一个textview.Now当第一个活动中的按钮按下Edittext中的文本时应转到第二个活动的textView我已尝试过如下代码,但它不起作用:

MainActivity.java

package com.example.myweb;

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.EditText;

public class MainActivity extends Activity {
     public final static String EXTRA_MESSAGE = "com.example.myweb";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button b =(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 EditText ed = (EditText)findViewById(R.id.edit_msg);
                Intent i = new Intent(getApplicationContext(),Act2.class);
            String s= ed.getText().toString();

            i.putExtra("EXTRA_MESSAGE", s);
                startActivity(i);

            }
        });

    }

}

Act2.java

package com.example.myweb;

import org.w3c.dom.Text;

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.EditText;
import android.widget.TextView;

public class Act2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            TextView tv = (TextView)findViewById(R.id.tv1); 
                Intent i =getIntent();
                String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
            tv.setText(msg);

            setContentView(tv);





            }
        });
    }
}

请帮帮我。谢谢你

9 个答案:

答案 0 :(得分:3)

您正在使用

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

所以,在MainActivity中

i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);

(OR)

String message = intent.getStringExtra("EXTRA_MESSAGE");

i.putExtra("EXTRA_MESSAGE", s);

答案 1 :(得分:3)

更改

String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);

String msg = i.getStringExtra("EXTRA_MESSAGE");

MainActivity.EXTRA_MESSAGE表示MainActivity.java类的静态变量。希望你能有所作为。您需要放入EXTRA_MESSAGE

中的变量intent i

编辑:对于您的崩溃,我们需要一个logcat o / p和activity,清单代码。但可能的原因是:

  1. Act2.java文件中未声明活动manifest

  2. 您说您在第二项活动中只有一个textview。但是你试图从button1获得act2.xml。所以你要靠近力量。

  3. Act2.java' s onCreate()更改为

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
    
        TextView tv = (TextView)findViewById(R.id.tv1); 
        Intent i =getIntent();
        String msg = i.getStringExtra("EXTRA_MESSAGE");
        tv.setText(msg);
    }
    

    您不需要调用setContentView(tv);,因为它已存在于act2.xml中并且您正在使用setContentView(R.layout.act2);

    希望你的问题得到解决。

答案 2 :(得分:3)

试试这个:

在MainActivity.java中:

将Button b和EditText声明为类字段(即将其保留在onCreate()之外)

class MainActivity.java
{
Button b;
EditText ed;
...

onCreate() {
...
        b =(Button)findViewById(R.id.button1);
ed = (EditText)findViewById(R.id.edit_msg);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent i = new Intent(MainActivity.this,Act2.class);
            String s= ed.getText().toString();

            i.putExtra("EXTRA_MESSAGE", s);
                startActivity(i);

            }
        });
...

在Act2.java中:

    ...
    Intent i = getIntent();
    String msg = i.getStringExtra("EXTRA_MESSAGE");


    TextView tv = (TextView)findViewById(R.id.tv1); 
tv.setText(msg);
     Button b1=(Button)findViewById(R.id.button1);
            b1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub



                }
            });
    ...

答案 3 :(得分:1)

    Use this code 

    **MainActivity.java**

        public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button)findViewById(R.id.button1);
        final EditText editText = (EditText)findViewById(R.id.editText1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                 intent.putExtra("EXTRA_MESSAGE", editText.getText().toString());
                startActivity(intent);

            }
        });


    }

}

**activity_main.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=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="74dp"
        android:layout_marginTop="26dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="69dp"
        android:layout_marginTop="47dp"
        android:text="Button" />

</RelativeLayout>

**SecondActivity.java**

public class SecondActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // TODO Auto-generated method stub

        setContentView(R.layout.second);

        Intent intent = getIntent();
        final String message = intent.getStringExtra("EXTRA_MESSAGE");

     Button button = (Button)findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                TextView textView = new TextView(SecondActivity.this);
                textView.setText(message);

                setContentView(textView);

            }
        });
    }

}


**second.xml** 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />



</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tester"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.tester.MainActivity"
            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=".SecondActivity"></activity>
    </application>

</manifest>

答案 4 :(得分:0)

尝试

Intent i = new Intent(MainActivity.this,Act2.class);

答案 5 :(得分:0)

您是否记得在清单XML中正确定义了第二个活动?

查看这本精彩指南,了解您可能遗漏的任何内容:http://developer.android.com/training/basics/firstapp/starting-activity.html

答案 6 :(得分:0)

您正在使用

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

所以,在MainActivity中

i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);

答案 7 :(得分:0)

MainActivity.java中EXTRA_MESSAGE变量的值与您将消息放入intent的键值不同。

MainActivity.java:

public final static String EXTRA_MESSAGE = "com.example.myweb";

i.put("EXTRA_MESSAGE",s);

Act2.java :(你正在通过extra_message变量访问,这是不正确的)

Intent i = getIntent();
String text = i.getStringExtra(MainActivity.EXTRA_MESSAGE); //WHICH IS DIFFER FROM THE KEY VALUE

因此,在设置和获取值时,键值应该相同。这里设置值键是“EXTRA_MESSAGE”,并且在获取“com.example.myweb”时,您将获得空指针异常。

解决方案:

只需更改Act2.java

中的行
Intent i = getIntent();
String text = i.getStringExtra("EXTRA_MESSAGE");

另请检查您的清单文件以获取活动声明。

希望它可以帮到你。

答案 8 :(得分:0)

请查看并更改您的代码

  package com.example.activityact;

  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 MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);


   Button b =(Button)findViewById(R.id.button1);
   b.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub

            EditText ed = (EditText)findViewById(R.id.editText1);
           Intent i = new Intent(getApplicationContext(),Act2.class);
       String s= ed.getText().toString();

       i.putExtra("EXTRA_MESSAGE", s);
           startActivity(i);

       }
   });

  }

 }

<强> Act2.java

package com.example.activityact;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Act2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act2);
    // TODO Auto-generated method stub
    TextView tv = (TextView) findViewById(R.id.textView1);
    Intent i =getIntent();
    // String msg = i.getStringExtra(EXTRA_MESSAGE);
    //String receiver = getIntent().getStringExtra(EXTRA_MESSAGE);
    String message = i.getStringExtra("EXTRA_MESSAGE");
    tv.setText(message);

}

 }