将数据从一个活动移动到另一个活

时间:2012-11-25 12:22:45

标签: android

我正在尝试将一些数据从一个活动移动到另一个活动。 使用类应用程序

项目活动:

  • Main_Activity。

  • Second_Activity。


主要活动代码

public class MainActivity extends Activity
{
    private ApplicationSettings application;
    private Button btn;
    private EditText txt1;
    private EditText txt2;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt1 = (EditText)findViewById(R.id.txt1);
        txt2 = (EditText)findViewById(R.id.txt2);
        btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                application = (ApplicationSettings)getApplication();
                application.setUserID(txt1.getText().toString());
                application.setUserName(txt2.getText().toString());
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

============================= 第二个活动代码

public class SecondActivity extends Activity 
{

    private ApplicationSettings application;
    private TextView txt1,txt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

           application = (ApplicationSettings)getApplication();

           txt1 = (TextView)findViewById(R.id.txt1);
           txt2 = (TextView)findViewById(R.id.txt2);

           txt1.setText(application.getUserID().toString());
           txt2.setText(application.getUserName().toString());
    }
}

============================== 申请类

public class ApplicationSettings extends Application
{


    String UserID;
    String UserName;

    public String getUserID()
    {
        return UserID;
    }
    public void setUserID(String userID)
    {
        UserID = userID;
    }
    public String getUserName()
    {
        return UserName;
    }
    public void setUserName(String userName)
    {
        UserName = userName;
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }   
}

=============================

LogCat

11-25 15:32:31.695: E/Trace(17282): error opening trace file: No such file or directory (2)
11-25 15:32:31.960: D/libEGL(17282): loaded /system/lib/egl/libEGL_mali.so
11-25 15:32:31.965: D/libEGL(17282): loaded /system/lib/egl/libGLESv1_CM_mali.so
11-25 15:32:31.965: D/libEGL(17282): loaded /system/lib/egl/libGLESv2_mali.so
11-25 15:32:31.975: D/(17282): Device driver API match
11-25 15:32:31.975: D/(17282): Device driver API version: 10
11-25 15:32:31.975: D/(17282): User space API version: 10 
11-25 15:32:31.975: D/(17282): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Wed Sep 12 17:53:53 KST 2012 
11-25 15:32:32.000: D/OpenGLRenderer(17282): Enabling debug mode 0
11-25 15:32:32.005: E/SensorManager(17282): thread start
11-25 15:32:32.010: D/SensorManager(17282): registerListener :: handle = 0  name= LSM330DLC Acceleration Sensor delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f68078
11-25 15:32:32.075: W/IInputConnectionWrapper(17282): showStatusIcon on inactive InputConnection
11-25 15:32:37.290: D/GestureDetector(17282): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
11-25 15:32:37.295: D/SensorManager(17282): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f68078
11-25 15:32:37.295: D/Sensors(17282): Remain listener = Sending .. normal delay 200ms
11-25 15:32:37.295: I/Sensors(17282): sendDelay --- 200000000
11-25 15:32:37.295: D/SensorManager(17282): JNI - sendDelay
11-25 15:32:37.295: I/SensorManager(17282): Set normal delay = true
11-25 15:32:37.300: D/SensorManager(17282): registerListener :: handle = 0  name= LSM330DLC Acceleration Sensor delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f69650
11-25 15:32:37.315: W/IInputConnectionWrapper(17282): getSelectedText on inactive InputConnection
11-25 15:32:37.315: W/IInputConnectionWrapper(17282): setComposingText on inactive InputConnection
11-25 15:32:37.315: W/IInputConnectionWrapper(17282): getExtractedText on inactive InputConnection
11-25 15:32:41.350: D/SensorManager(17282): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f69650
11-25 15:32:41.350: D/Sensors(17282): Remain listener = Sending .. normal delay 200ms
11-25 15:32:41.350: I/Sensors(17282): sendDelay --- 200000000
11-25 15:32:41.350: D/SensorManager(17282): JNI - sendDelay
11-25 15:32:41.350: I/SensorManager(17282): Set normal delay = true
11-25 15:32:41.620: W/IInputConnectionWrapper(17282): showStatusIcon on inactive InputConnection
11-25 15:32:46.200: D/SensorManager(17282): registerListener :: handle = 0  name= LSM330DLC Acceleration Sensor delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f8b4c8
11-25 15:32:47.170: D/GestureDetector(17282): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
11-25 15:32:47.175: D/SensorManager(17282): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f8b4c8
11-25 15:32:47.175: D/Sensors(17282): Remain listener = Sending .. normal delay 200ms
11-25 15:32:47.175: I/Sensors(17282): sendDelay --- 200000000
11-25 15:32:47.175: D/SensorManager(17282): JNI - sendDelay
11-25 15:32:47.175: I/SensorManager(17282): Set normal delay = true
11-25 15:32:47.180: D/SensorManager(17282): registerListener :: handle = 0  name= LSM330DLC Acceleration Sensor delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f8c6b8
11-25 15:32:47.210: W/IInputConnectionWrapper(17282): getSelectedText on inactive InputConnection
11-25 15:32:47.210: W/IInputConnectionWrapper(17282): setComposingText on inactive InputConnection
11-25 15:32:47.210: W/IInputConnectionWrapper(17282): getExtractedText on inactive InputConnection
11-25 15:32:50.470: D/GestureDetector(17282): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
11-25 15:32:50.475: D/SensorManager(17282): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f8c6b8
11-25 15:32:50.475: D/Sensors(17282): Remain listener = Sending .. normal delay 200ms
11-25 15:32:50.475: I/Sensors(17282): sendDelay --- 200000000
11-25 15:32:50.475: D/SensorManager(17282): JNI - sendDelay
11-25 15:32:50.475: I/SensorManager(17282): Set normal delay = true
11-25 15:32:50.480: D/SensorManager(17282): registerListener :: handle = 0  name= LSM330DLC Acceleration Sensor delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41f8b4c8
11-25 15:32:50.490: W/IInputConnectionWrapper(17282): getSelectedText on inactive InputConnection
11-25 15:32:50.490: W/IInputConnectionWrapper(17282): setComposingText on inactive InputConnection
11-25 15:32:50.490: W/IInputConnectionWrapper(17282): getExtractedText on inactive InputConnection
11-25 15:32:53.000: D/GestureDetector(17282): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
11-25 15:32:53.160: D/AndroidRuntime(17282): Shutting down VM
11-25 15:32:53.160: W/dalvikvm(17282): threadid=1: thread exiting with uncaught exception (group=0x412862a0)
11-25 15:32:53.175: E/AndroidRuntime(17282): FATAL EXCEPTION: main
11-25 15:32:53.175: E/AndroidRuntime(17282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test01/com.example.test01.SecondActivity}: java.lang.NullPointerException
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.os.Looper.loop(Looper.java:137)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at java.lang.reflect.Method.invoke(Method.java:511)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at dalvik.system.NativeStart.main(Native Method)
11-25 15:32:53.175: E/AndroidRuntime(17282): Caused by: java.lang.NullPointerException
11-25 15:32:53.175: E/AndroidRuntime(17282):    at com.example.test01.SecondActivity.onCreate(SecondActivity.java:26)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.Activity.performCreate(Activity.java:5191)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-25 15:32:53.175: E/AndroidRuntime(17282):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-25 15:32:53.175: E/AndroidRuntime(17282):    ... 11 more
11-25 15:32:53.195: D/dalvikvm(17282): GC_CONCURRENT freed 180K, 9% free 12405K/13575K, paused 12ms+2ms, total 31ms

我需要将数据从主活动的DditTexts传递到Second_activity中的TextView

提前致谢

此致

1 个答案:

答案 0 :(得分:3)

作为您的日志:

  

您是否在AndroidManifest.xml中声明了此活动?

表示确保您已将AndroidManifest中的SecondActivity.class活动声明为:

<Activity android:name=".SecondActivity" />

如果您已声明它,请确保它与您在SecondActivity.java

中创建的包在同一个包中或具有相同的名称

如果您在不同的包中有SecondActivity,请在AndroidManifest中将其声明为:

<Activity android:name="<FULL_PACKAGE_NAME>.SecondActivity" />