致命异常:E/AndroidRuntime(31558):由以下原因引起:java.lang.NullPointerException

致命异常:E/AndroidRuntime(31558):由以下原因引起:java.lang.NullPointerException,java,android,android-layout,android-intent,android-fragments,Java,Android,Android Layout,Android Intent,Android Fragments,我是Android新手,尤其是在fragment。 我正在尝试创建一个包含2个活动的简单应用程序。第一个(MainActivity)发送一条来自EditText(fragment_main_activity)的消息,并将其带到第二个(SecondActivity)并使用textView(fragment_second_activity)对其进行解析 我尝试了一些新的解决方案,从其他问题和建议遍布整个网站。我明白,问题是围绕着我们的看法 我尝试删除textView,并用一个简单的Toast来释放消

我是Android新手,尤其是在fragment。 我正在尝试创建一个包含2个活动的简单应用程序。第一个(MainActivity)发送一条来自EditText(fragment_main_activity)的消息,并将其带到第二个(SecondActivity)并使用textView(fragment_second_activity)对其进行解析

我尝试了一些新的解决方案,从其他问题和建议遍布整个网站。我明白,问题是围绕着我们的看法

我尝试删除textView,并用一个简单的Toast来释放消息。效果非常好。下面是我在secondActivity.java中的onCreate方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();

        }

        setContentView(R.layout.activity_second);


        Intent intentMessaggio = getIntent();
        String messaggioRicevuto = intentMessaggio.getStringExtra(getPackageName().concat(MainActivity.KEY_INTENT_MESSAGGIO));

        Toast.makeText(getApplicationContext(), messaggioRicevuto, Toast.LENGTH_LONG).show();

//      TextView textView1 = (TextView)findViewById(R.id.textView1);
//      textView1.append(messaggioRicevuto);
    }
以下是日志:

05-07 21:37:44.740: E/AndroidRuntime(971): FATAL EXCEPTION: main
05-07 21:37:44.740: E/AndroidRuntime(971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app_01/com.example.app_01.SecondActivity}: java.lang.NullPointerException
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.ActivityThread.access$700(ActivityThread.java:159)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.os.Looper.loop(Looper.java:176)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.ActivityThread.main(ActivityThread.java:5419)
05-07 21:37:44.740: E/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)
05-07 21:37:44.740: E/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:525)
05-07 21:37:44.740: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-07 21:37:44.740: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-07 21:37:44.740: E/AndroidRuntime(971):  at dalvik.system.NativeStart.main(Native Method)
05-07 21:37:44.740: E/AndroidRuntime(971): Caused by: java.lang.NullPointerException
05-07 21:37:44.740: E/AndroidRuntime(971):  at com.example.app_01.SecondActivity.onCreate(SecondActivity.java:39)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.Activity.performCreate(Activity.java:5372)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-07 21:37:44.740: E/AndroidRuntime(971):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
05-07 21:37:44.740: E/AndroidRuntime(971):  ... 11 more
我试图在第二个活动的第39行更改一些内容,但我无法解决问题

以下是我的主要活动:

package com.example.app_01;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;

 public class MainActivity extends Activity {

final static String KEY_INTENT_MESSAGGIO=".KEY_INTENT_MESSAGGIO";



public void inviaMessaggio(View view){

Intent intentMessaggio = new Intent(this,SecondActivity.class);

    EditText editText1 = (EditText)findViewById(R.id.editText1);
    String messaggioRicevuto = editText1.getText().toString();
    intentMessaggio.putExtra(getPackageName().concat(KEY_INTENT_MESSAGGIO),messaggioRicevuto);
    startActivity(intentMessaggio);
}


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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }



}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}
}

这是我的活动单元主布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app_01.MainActivity"
tools:ignore="MergeRootFrame" />
}

这是我的activity_second.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app_01.SecondActivity"
tools:ignore="MergeRootFrame" />
这是我的清单:

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

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

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

</manifest>


谢谢你的帮助。我不打算构建一个应用程序,但我对解决这个问题很感兴趣。

导致空指针的一行是:

textView1.append(messaggioRicevuto);
因此,
textView1
null


您的问题是活动布局不包含对
R.id.textView1
的引用,而是在片段中引用此文本视图,因此:如果要使用它,请将代码移动到片段类或将文本视图移动到活动布局(
activity\u second.xml
).

第二个活动的第39行是什么?哇,考虑到你的日志已经告诉你问题出在哪里,代码太多了。您的textview1似乎为空。如何将代码移动到片段类?我想您是在谈论我的SecondActivity的onCreateView中的类占位符片段。是否可以在SecondActivity中更改我的setContentView?并不是指我的second_activity_布局,而是指我的fragment_second_活动?是的,这是可能的,但如果不想使用片段,我建议将XML内容移动到second_activity.XML。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.app_01.SecondActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="104dp"
    android:text="@string/editText_inserisciIlMessaggio"/>
    /* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.app_01;

public final class R {
    public static final class attr {
    }
    public static final class dimen {
        /**  Default screen margins, per the Android Design guidelines. 

         Example customization of dimensions originally defined in res/values/dimens.xml
         (such as screen margins) for screens with more than 820dp of available width. This
         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).

         */
        public static final int activity_horizontal_margin=0x7f040000;
        public static final int activity_vertical_margin=0x7f040001;
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int action_settings=0x7f080004;
        public static final int button1=0x7f080002;
        public static final int container=0x7f080000;
        public static final int editText1=0x7f080001;
        public static final int textView1=0x7f080003;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
        public static final int activity_second=0x7f030001;
        public static final int fragment_main=0x7f030002;
        public static final int fragment_second=0x7f030003;
    }
    public static final class menu {
        public static final int main=0x7f070000;
        public static final int second=0x7f070001;
    }
    public static final class string {
        public static final int action_settings=0x7f050001;
        public static final int app_name=0x7f050000;
        public static final int button_invia=0x7f050003;
        public static final int editText_inserisciIlMessaggio=0x7f050004;
        public static final int title_activity_second=0x7f050002;
    }
    public static final class style {
        /** 
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.


        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.

 API 11 theme customizations can go here. 

        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.

 API 14 theme customizations can go here. 
         */
        public static final int AppBaseTheme=0x7f060000;
        /**  Application theme. 
 All customizations that are NOT specific to a particular API-level can go here. 
         */
        public static final int AppTheme=0x7f060001;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app_01"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

</manifest>
textView1.append(messaggioRicevuto);