尝试从一个活动中获取文本并打印到文本活动但它失败了

时间:2013-05-19 14:47:43

标签: android android-intent

下面的代码中没有错误,第一个活动效果很好,但点击按钮就停止了(崩溃)

帮我解决这个问题

public class MainActivity extends Activity {

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

    final EditText et = (EditText) findViewById(R.id.textView1);

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

    public void onClick(View v) {

        Intent intent =new Intent(MainActivity.this, Second.class);
        intent.putExtra("thetv",et.getText().toString() );
        startActivity(intent);
    }
});
}    
}

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second) ;           
            TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText(getIntent().getExtras().getString("thetv"));
}
}

logcat数据

05-19 21:51:06.089:W / KeyCharacterMap(1561):id 0没有键盘

05-19 21:51:06.089:W / KeyCharacterMap(1561):使用默认键映射:/system/usr/keychars/qwerty.kcm.bin

05-19 21:51:15.659:D / AndroidRuntime(1561):关闭虚拟机

05-19 21:51:15.659:W / dalvikvm(1561):threadid = 1:线程退出时未捕获异常(组= 0x40015560)

05-19 21:51:15.669:E / AndroidRuntime(1561):致命异常:主

05-19 21:51:15.669:E / AndroidRuntime(1561):java.lang.NullPointerException

05-19 21:51:15.669:E / AndroidRuntime(1561):at com.nav.activitieslearn.MainActivity $ 1.onClick(MainActivity.java:26)

05-19 21:51:15.669:E / AndroidRuntime(1561):在android.view.View.performClick(View.java:2485)

05-19 21:51:15.669:E / AndroidRuntime(1561):在android.view.View $ PerformClick.run(View.java:9080)

05-19 21:51:15.669:E / AndroidRuntime(1561):在android.os.Handler.handleCallback(Handler.java:587)

05-19 21:51:15.669:E / AndroidRuntime(1561):在android.os.Handler.dispatchMessage(Handler.java:92)

05-19 21:51:15.669:E / AndroidRuntime(1561):在android.os.Looper.loop(Looper.java:123)

05-19 21:51:15.669:E / AndroidRuntime(1561):在android.app.ActivityThread.main(ActivityThread.java:3683)

05-19 21:51:15.669:E / AndroidRuntime(1561):at java.lang.reflect.Method.invokeNative(Native Method)

05-19 21:51:15.669:E / AndroidRuntime(1561):at java.lang.reflect.Method.invoke(Method.java:507)

05-19 21:51:15.669:E / AndroidRuntime(1561):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)

05-19 21:51:15.669:E / AndroidRuntime(1561):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

05-19 21:51:15.669:E / AndroidRuntime(1561):at dalvik.system.NativeStart.main(Native Method)

05-19 21:51:19.538:I / Process(1561):发送信号。 PID:1561 SIG:9

android manifest

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

    </activity>

</application>

3 个答案:

答案 0 :(得分:1)

在我看来,EditText的名称不是textView1。

答案 1 :(得分:0)

活动1:

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

public class Activity1 extends Activity {

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

         Intent intent =new Intent(Activity1.this, Activity2.class);
            intent.putExtra("Key","Sringtosecondactivity" );
            startActivity(intent);

    }

}

活动2

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

public class Activity2 extends Activity {

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

       System.out.println("EEEEEEEEEEEEEEEEEe "+getIntent().getExtras().getString("Key"));


    }
}

清单:

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Activity1" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:label="@string/app_name"
            android:name=".Activity2" >
            </activity>
    </application>

</manifest>

答案 2 :(得分:0)

检查这些行,

活动1:

final EditText et = (EditText) findViewById(R.id.textView1);

在活动2中:

TextView tv = (TextView) findViewById(R.id.textView1);

应该(我猜):

活动1:

final EditText et = (EditText) findViewById(R.id.editText1);

在活动2中:

TextView tv = (TextView) findViewById(R.id.textView1);

尝试此修正并查看其是否有效。