使用Intents,Android在活动之间移动

时间:2013-03-23 21:58:21

标签: android android-intent android-activity

我正在尝试构建一个包含两个活动的简单应用程序。第一项活动有2个ButtonsEditText字段。当按save&move时,第二个活动应该打开并显示在EditText字段中输入的文本(在第一个活动中)。按下just move按钮时,第二个活动应该加载(而不是做任何事情)。出于某种原因,当按下两个按钮时 没有任何反应

*对不起,如果按钮的名称不是很清楚。提前谢谢!

下面是第一项活动的代码:

import android.app.Activity;
import android.content.Context;
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;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{

    private Button nextBT;
    private EditText mainEditText;
    private String data;
    private Button nextPref;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nextBT=(Button)findViewById(R.id.nextButton);
        mainEditText=(EditText)findViewById(R.id.dataEditText);
        nextPref=(Button)findViewById(R.id.nextPrefBut);        
    }

    public void onClick(View v)
    {
        int id = v.getId();

        switch(id)
        {
            case R.id.nextButton:
            {
                data=mainEditText.getText().toString();
                if(!(data.equals("")) && (data!=null) )
                {
                    Intent intent = new Intent(MainActivity.this,Other.class);
                    intent.putExtra("Intent_Data", data);
                    startActivity(intent);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "no data to pass", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case R.id.nextPrefBut:
            {
                startActivity(new Intent(MainActivity.this, Other.class));  
                break;
            }
        }
    }
}

继承第二项活动的代码:

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

public class Other extends Activity
{
    private TextView textFromMain;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        textFromMain = (TextView)findViewById(R.id.mainText);
        textFromMain.setText(getIntent().getExtras().getString("Intent_Data"));

    }
}

编辑:

感谢您的回复。新问题,如果按下just move,应用程序会崩溃,有什么想法吗?第一个按钮save&move工作得很好。

编辑#2,来自崩溃的logcat:

03-24 12:50:14.135: I/ActivityManager(289): START u0 {cmp=com.example.c/.Other} from pid 851
03-24 12:50:14.314: D/dalvikvm(289): GC_FOR_ALLOC freed 980K, 20% free 8310K/10384K, paused 99ms, total 113ms
03-24 12:50:14.584: D/AndroidRuntime(851): Shutting down VM
03-24 12:50:14.584: W/dalvikvm(851): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-24 12:50:14.625: E/AndroidRuntime(851): FATAL EXCEPTION: main
03-24 12:50:14.625: E/AndroidRuntime(851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.c/com.example.c.Other}: java.lang.NullPointerException
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.os.Looper.loop(Looper.java:137)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-24 12:50:14.625: E/AndroidRuntime(851):  at java.lang.reflect.Method.invokeNative(Native Method)
03-24 12:50:14.625: E/AndroidRuntime(851):  at java.lang.reflect.Method.invoke(Method.java:511)
03-24 12:50:14.625: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-24 12:50:14.625: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-24 12:50:14.625: E/AndroidRuntime(851):  at dalvik.system.NativeStart.main(Native Method)
03-24 12:50:14.625: E/AndroidRuntime(851): Caused by: java.lang.NullPointerException
03-24 12:50:14.625: E/AndroidRuntime(851):  at com.example.c.Other.onCreate(Other.java:16)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.Activity.performCreate(Activity.java:5104)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-24 12:50:14.625: E/AndroidRuntime(851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-24 12:50:14.625: E/AndroidRuntime(851):  ... 11 more
03-24 12:50:14.664: W/ActivityManager(289):   Force finishing activity com.example.c/.Other
03-24 12:50:14.703: W/ActivityManager(289):   Force finishing activity com.example.c/.MainActivity
03-24 12:50:14.974: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-24 12:50:15.216: W/ActivityManager(289): Activity pause timeout for ActivityRecord{4112d850 u0 com.example.c/.Other}
03-24 12:50:15.623: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-24 12:50:15.643: W/EGL_emulation(413): eglSurfaceAttrib not implemented
03-24 12:50:16.443: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:16.643: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:16.753: I/Process(851): Sending signal. PID: 851 SIG: 9
03-24 12:50:16.773: I/WindowState(289): WIN DEATH: Window{41134870 u0 com.example.c/com.example.c.MainActivity}
03-24 12:50:16.786: I/ActivityManager(289): Process com.example.c (pid 851) has died.
03-24 12:50:16.943: W/InputMethodManagerService(289): Got RemoteException sending setActive(false) notification to pid 851 uid 10052
03-24 12:50:17.156: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:17.233: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:17.366: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:46.890: D/ExchangeService(658): Received deviceId from Email app: null

4 个答案:

答案 0 :(得分:2)

您还没有为按钮设置onClickListener。

MainActivity onCreate方法中添加此内容。

 nextBT.setOnClickListener(this);
nextPref.setOnClickListener(this);

答案 1 :(得分:1)

首先,您没有为按钮设置单击侦听器。添加:

nextBT.setOnClickListener(this);
nextPref.setOnClickListener(this);

对于两个,您的switch-case语法错误。 case块不需要括号。尝试:

    switch(id)
    {
        case R.id.nextButton:

            data=mainEditText.getText().toString();
            if(!(data.equals("")) && (data!=null) )
            {
                Intent intent = new Intent(MainActivity.this,Other.class);
                intent.putExtra("Intent_Data", data);
                startActivity(intent);
            }
            else
            {
                Toast.makeText(getApplicationContext(), "no data to pass", Toast.LENGTH_SHORT).show();
            }
            break;

        case R.id.nextPrefBut:

            startActivity(new Intent(MainActivity.this, Other.class));  
            break;

    }

答案 2 :(得分:1)

为清楚起见,我将尝试总结讨论和总结的内容:

1)初始问题(按钮是非反应性)是由于他们没有注册任何听众而引起的。为了使任何小部件(Button,textview等)以某种非默认方式作出反应,它必须注册一个监听器,该监听器将负责该反应。在这种情况下,解决方案只是在OnCreate的底部添加以下两行代码:

nextBT.setOnClickListener(this);
nextPref.setOnClickListener(this);

2)just move课程中出现第二个问题(当用户按下Other按钮时应用程序崩溃)。我们看到在MainActivity中,当用户按下save&move按钮时,会执行以下行:

intent.putExtra("Intent_Data", data);

但是,当用户按下just move按钮时,不会执行相应的操作。结果是有时意图将附加到Intent_Data键的值,有时它不会,这取决于按下哪个按钮。由于Other中未执行任何检查,因此要查看Intent_Data是否具有值(即getIntent().getExtras().getString("Intent_Data") != null),每次按下just move按钮时都会出现NullPointerException。这个问题有很多简单的解决方案。例如,您可以添加上述检查。然而,最好的解决方案是保持意图一致,并简单地重写onClick方法,如下所示:

public void onClick(View v)
{
    String data = mainEditText.getText().toString();
    if(v.getId() == R.id.nextButton && data.equals("")) {
        Toast.makeText(getApplicationContext(), "no data to pass", Toast.LENGTH_SHORT).show();
    }
    else {
        Intent intent = new Intent(MainActivity.this,Other.class);
        intent.putExtra("Intent_Data", data);
        startActivity(intent);
    }
}

此外,请考虑使用以下功能:getStringExtra("Intent_Data")而不是使用:getExtras().getString("Intent_Data")。它做同样的事情,但它更短,更有效。

答案 3 :(得分:0)

来源类:

    Intent myIntent = new Intent(this, NewActivity.class); 
    myIntent.putExtra("firstName", "Your First Name Here");
    myIntent.putExtra("lastName", "Your Last Name Here");
    startActivity(myIntent)

目标类(NewActivity类):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);    

    Intent intent = getIntent();

    String fName = intent.getStringExtra("firstName");
    String lName = intent.getStringExtra("lastName");

  }