-1

请帮助我。我是 android 的初学者。我有一个 android 应用程序。我有一个按钮,可以编辑文本,我想要我在文本中写的每一件事,用 putextra 发送到另一个页面并在 textView 中显示它。但是当我点击这个按钮时,我的应用程序停止并说:对不起。应用程序已意外停止。请再试一次。谢谢你的关注。这是日志猫错误:

08-16 17:17:30.001: E/AndroidRuntime(736): FATAL EXCEPTION: main
08-16 17:17:30.001: E/AndroidRuntime(736): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapplicationandroid/com.example.secondapplicationandroid.dovomi}: java.lang.NullPointerException
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.os.Looper.loop(Looper.java:123)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 17:17:30.001: E/AndroidRuntime(736):  at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:17:30.001: E/AndroidRuntime(736):  at java.lang.reflect.Method.invoke(Method.java:507)
08-16 17:17:30.001: E/AndroidRuntime(736):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 17:17:30.001: E/AndroidRuntime(736):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 17:17:30.001: E/AndroidRuntime(736):  at dalvik.system.NativeStart.main(Native Method)
08-16 17:17:30.001: E/AndroidRuntime(736): Caused by: java.lang.NullPointerException
08-16 17:17:30.001: E/AndroidRuntime(736):  at com.example.secondapplicationandroid.dovomi.onCreate(dovomi.java:14)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:17:30.001: E/AndroidRuntime(736):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-16 17:17:30.001: E/AndroidRuntime(736):  ... 11 more
08-16 17:17:33.012: I/Process(736): Sending signal. PID: 736 SIG: 9
08-16 17:21:48.723: W/KeyCharacterMap(778): No keyboard for id 0
08-16 17:21:48.723: W/KeyCharacterMap(778): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-16 17:21:49.891: D/AndroidRuntime(778): Shutting down VM
08-16 17:21:49.891: W/dalvikvm(778): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-16 17:21:49.921: E/AndroidRuntime(778): FATAL EXCEPTION: main
08-16 17:21:49.921: E/AndroidRuntime(778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapplicationandroid/com.example.secondapplicationandroid.dovomi}: java.lang.NullPointerException
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.os.Looper.loop(Looper.java:123)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 17:21:49.921: E/AndroidRuntime(778):  at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:21:49.921: E/AndroidRuntime(778):  at java.lang.reflect.Method.invoke(Method.java:507)
08-16 17:21:49.921: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 17:21:49.921: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 17:21:49.921: E/AndroidRuntime(778):  at dalvik.system.NativeStart.main(Native Method)
08-16 17:21:49.921: E/AndroidRuntime(778): Caused by: java.lang.NullPointerException
08-16 17:21:49.921: E/AndroidRuntime(778):  at com.example.secondapplicationandroid.dovomi.onCreate(dovomi.java:21)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:21:49.921: E/AndroidRuntime(778):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-16 17:21:49.921: E/AndroidRuntime(778):  ... 11 more
08-16 17:26:50.131: I/Process(778): Sending signal. PID: 778 SIG: 9

好的。我的 MainActivity.java 是:

package com.example.secondapplicationandroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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 {

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

        Button b= (Button) findViewById(R.id.button2);
        final EditText matn = (EditText) findViewById(R.id.editText1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String s=matn.getText().toString();
                Intent mafhoom=new Intent(MainActivity.this, dovomi.class);
                mafhoom.putExtra("varedshode", s);
                startActivity(mafhoom);
                //startActivity(new Intent(MainActivity.this, dovomi.class));
            }//end onclick
        });

    }//end void onCreate


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

}

and my second page that named is,dovomi.java is:

package com.example.secondapplicationandroid;

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

public class dovomi extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        TextView matni=(TextView) findViewById(R.id.textView2);
        Intent iin= getIntent();
        Bundle b = iin.getExtras();
        if(b!=null)
        {
            String j =(String) b.get("varedshode");
            matni.setText(j);
        }

        //matni.setText(getIntent().getExtras().getString("varedshode"));
        setContentView(R.layout.dovomi);
    }
}

清单是:

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

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

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

</manifest>

对于图层,dovomi.xml:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/salam" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
4

1 回答 1

1

问题是线路TextView matni=(TextView) findViewById(R.id.textView2);

在未指定视图的 Activity 调用findViewById中检查当前 Activity 的setContentView视图。问题是你在打电话setContentView(R.layout.dovomi);后打电话findviewbyid

只要确保setContentView()在任何基本调用之前findViewById调用。

所以在你的第二堂课setContentView(R.layout.dovomi);super.onCreate(savedInstanceState);

于 2013-08-18T05:15:53.677 回答