1

我正在尝试构建一个包含两个活动的简单应用程序。第一个活动有 2Buttons和一个EditText字段。当按下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

4 回答 4

2

您尚未为按钮设置 onClickListener。

IE

将此添加到MainActivity onCreate 方法中。

 nextBT.setOnClickListener(this);
nextPref.setOnClickListener(this);
于 2013-03-23T21:59:34.413 回答
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;

    }
于 2013-03-23T22:02:41.583 回答
1

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

1)最初的问题(按钮没有反应)是因为他们没有注册任何听众。为了让任何小部件(按钮、文本视图等)以某种非默认方式做出反应,它必须注册一个侦听器,该侦听器将负责该反应。在这种情况下,解决方案只是在底部添加以下两行代码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")。它做同样的事情,但它更短更高效。

于 2013-03-25T08:57:13.587 回答
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");

  }
于 2014-02-06T12:35:46.620 回答