Android无法实例化活动组件信息

Android无法实例化活动组件信息,android,android-intent,Android,Android Intent,我在这里找到了一些类似帖子的回复,但是没有一个能够解决我的问题。我有一个带有两个活动的简单应用程序。在EntryActivity中,值被输入到EditText中,然后显示在DisplayActivity中。我相当肯定这两个活动在清单中都被正确分配,但它会在活动之间停止工作,导致标题错误。我试过搬几样东西,但一点运气都没有 入口活动: package com.natep.userentry; import android.os.Bundle; import android.app.Activity

我在这里找到了一些类似帖子的回复,但是没有一个能够解决我的问题。我有一个带有两个活动的简单应用程序。在EntryActivity中,值被输入到EditText中,然后显示在DisplayActivity中。我相当肯定这两个活动在清单中都被正确分配,但它会在活动之间停止工作,导致标题错误。我试过搬几样东西,但一点运气都没有

入口活动:

package com.natep.userentry;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.Button;

public class EntryActivity extends Activity {

//Instantiate variables
public String sex = "";
public String firstName = "";
public String lastName = "";
public String age = "";

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

    //Instantiate the EditTexts and assign listeners
    EditText firstNameEditText = (EditText) findViewById(R.id.firstNameEditText);
    firstNameEditText.addTextChangedListener(firstNameEditTextListener);

    EditText lastNameEditText = (EditText) findViewById(R.id.lastNameEditText);
    lastNameEditText.addTextChangedListener(lastNameEditTextListener);

    EditText ageEditText = (EditText) findViewById(R.id.ageEditText);
    ageEditText.addTextChangedListener(ageEditTextListener);

    RadioButton radio_male = (RadioButton) findViewById(R.id.radio_male);
    RadioButton radio_female = (RadioButton) findViewById(R.id.radio_female);
    Button enterButton = (Button) findViewById(R.id.enterButton);
}

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

//Method for handling radio button input
public void radioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_male:
            if (checked)
                sex = "Male";
            break;
        case R.id.radio_female:
            if (checked)
                sex = "Female";
            break;
    }
}

//Handles the First Name EditText
private TextWatcher firstNameEditTextListener = new TextWatcher() 
   {
          @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) 
         {       

              try
              {
                  firstName = s.toString();
              } 
              catch (NumberFormatException e)
              {
                  firstName = "";
              }                             
          }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0)
        {

        }
    };

  //Handles the Last Name EditText
    private TextWatcher lastNameEditTextListener = new TextWatcher() 
       {
              @Override
             public void onTextChanged(CharSequence s, int start, int before, int count) 
             {       

                  try
                  {
                      lastName = s.toString();
                  } 
                  catch (NumberFormatException e)
                  {
                      lastName = "";
                  }                             
              }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0)
            {

            }
        };

      //Handles the Age EditText
        private TextWatcher ageEditTextListener = new TextWatcher() 
           {
                  @Override
                 public void onTextChanged(CharSequence s, int start, int before, int count) 
                 {       

                      try
                      {
                          age = s.toString();
                      } 
                      catch (NumberFormatException e)
                      {
                          age = "";
                      }                             
                  }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0)
                {

                }
            };

//Define method to create intent on Button click
public void enterData(View view) {
    Intent intent = new Intent(this, DisplayActivity.class);
    intent.putExtra("FIRST_NAME", firstName);
    intent.putExtra("LAST_NAME", lastName);
    intent.putExtra("USER_AGE", age);
    intent.putExtra("USER_SEX", sex) ;
    startActivity(intent);
}

}
显示活动

package com.natep.userentry;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class DisplayActivity extends Activity {

//Instantiate the necessary TextViews
TextView firstTextView = (TextView) findViewById(R.id.firstTextView);
TextView lastTextView = (TextView) findViewById(R.id.lastTextView);
TextView ageDisplayTextView = (TextView) findViewById(R.id.ageTextView);
TextView sexDisplayTextView = (TextView) findViewById(R.id.sexDisplayTextView);

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

    //Receive the bundle from Entry activity
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();

    //Instantiate strings from Entry activity
    String firstName = extras.getString("FIRST_NAME");
    String lastName = extras.getString("LAST_NAME");
    String age = extras.getString("USER_AGE");
    String sex = extras.getString("USER_SEX");

    //Populate the TextViews with strings from Bundle
    firstTextView.setText(firstName);
    lastTextView.setText(lastName);
    ageDisplayTextView.setText(age);
    sexDisplayTextView.setText(sex);

}

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

}
Manifest.xml:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.natep.userentry.EntryActivity"
            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.natep.userentry.DisplayActivity"
            android:label="@string/title_activity_display" >
        </activity>
    </application>

</manifest>

(编辑)错误日志,按要求

05-03 16:32:14.226: D/dalvikvm(1464): GC_FOR_ALLOC freed 59K, 6% free 2894K/3048K,                 paused 40ms, total 41ms
05-03 16:32:14.226: I/dalvikvm-heap(1464): Grow heap (frag case) to 3.497MB for 614416- byte allocation
05-03 16:32:14.296: D/dalvikvm(1464): GC_FOR_ALLOC freed 2K, 5% free 3491K/3652K,  paused 59ms, total 59ms
05-03 16:32:14.856: D/gralloc_goldfish(1464): Emulator without GPU emulation detected.
05-03 16:32:23.506: W/IInputConnectionWrapper(1464): endBatchEdit on inactive InputConnection
05-03 16:32:26.146: I/Choreographer(1464): Skipped 30 frames!  The application may be doing too much work on its main thread.
05-03 16:32:31.886: D/AndroidRuntime(1464): Shutting down VM
05-03 16:32:31.886: W/dalvikvm(1464): threadid=1: thread exiting with uncaught e     exception (group=0xb3a14ba8)
05-03 16:32:31.926: E/AndroidRuntime(1464): FATAL EXCEPTION: main
05-03 16:32:31.926: E/AndroidRuntime(1464): Process: com.natep.userentry, PID: 1464
05-03 16:32:31.926: E/AndroidRuntime(1464): java.lang.RuntimeException: Unable to  instantiate activity   ComponentInfo{com.natep.userentry/com.natep.userentry.DisplayActivity}: java.lang.NullPointerException
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.os.Looper.loop(Looper.java:136)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at  java.lang.reflect.Method.invokeNative(Native Method)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:515)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at dalvik.system.NativeStart.main(Native Method)
05-03 16:32:31.926: E/AndroidRuntime(1464): Caused by: java.lang.NullPointerException
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.Activity.findViewById(Activity.java:1884)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at com.natep.userentry.DisplayActivity.<init>(DisplayActivity.java:12)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at java.lang.Class.newInstanceImpl(Native Method)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at java.lang.Class.newInstance(Class.java:1208)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-03 16:32:31.926: E/AndroidRuntime(1464):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-03 16:32:31.926: E/AndroidRuntime(1464):     ... 11 more
05-03 16:32:35.216: I/Process(1464): Sending signal. PID: 1464 SIG: 9
05-03 16:32:14.226:D/dalvikvm(1464):释放59K,6%释放2894K/3048K,暂停40ms,总计41ms
05-03 16:32:14.226:I/dalvikvm堆(1464):为614416字节分配将堆(frag案例)增长到3.497MB
5-03 16:32:14.296:D/dalvikvm(1464):释放2K的所有物质的GC_,5%的自由3491K/3652K,暂停59ms,总计59ms
05-03 16:32:14.856:D/gralloc_金鱼(1464):未检测到GPU仿真的仿真器。
05-03 16:32:23.506:W/IIInputConnectionWrapper(1464):在非活动InputConnection上进行endBatchEdit
05-03 16:32:26.146:I/编舞(1464):跳过了30帧!应用程序可能在其主线程上做了太多工作。
05-03 16:32:31.886:D/AndroidRuntime(1464):关闭虚拟机
05-03 16:32:31.886:W/dalvikvm(1464):threadid=1:线程以未捕获的e异常退出(组=0xb3a14ba8)
05-03 16:32:31.926:E/AndroidRuntime(1464):致命异常:主
05-03 16:32:31.926:E/AndroidRuntime(1464):进程:com.natep.userentry,PID:1464
05-03 16:32:31.926:E/AndroidRuntime(1464):java.lang.RuntimeException:无法实例化活动组件信息{com.natep.userentry/com.natep.userentry.DisplayActivity}:java.lang.NullPointerException
05-03 16:32:31.926:E/AndroidRuntime(1464):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)上
05-03 16:32:31.926:E/AndroidRuntime(1464):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 16:32:31.926:E/AndroidRuntime(1464):在android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 16:32:31.926:E/AndroidRuntime(1464):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于android.os.Handler.dispatchMessage(Handler.java:102)
05-03 16:32:31.926:E/AndroidRuntime(1464):在android.os.Looper.loop(Looper.java:136)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于java.lang.reflect.Method.Invokenactive(本机方法)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于java.lang.reflect.Method.invoke(Method.java:515)
05-03 16:32:31.926:E/AndroidRuntime(1464):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
5-03 16:32:31.926:E/AndroidRuntime(1464):在dalvik.system.NativeStart.main(本机方法)
05-03 16:32:31.926:E/AndroidRuntime(1464):由以下原因引起:java.lang.NullPointerException
05-03 16:32:31.926:E/AndroidRuntime(1464):在android.app.Activity.findViewById(Activity.java:1884)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于com.natep.userentry.DisplayActivity。(DisplayActivity.java:12)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于java.lang.Class.newInstanceImpl(本机方法)
05-03 16:32:31.926:E/AndroidRuntime(1464):位于java.lang.Class.newInstance(Class.java:1208)
05-03 16:32:31.926:E/AndroidRuntime(1464):在android.app.Instrumentation.newActivity(Instrumentation.java:1061)上
05-03 16:32:31.926:E/AndroidRuntime(1464):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)上
05-03 16:32:31.926:E/AndroidRuntime(1464):。。。还有11个
05-03 16:32:35.216:I/进程(1464):发送信号。PID:1464信号:9

您之所以获得NPE,是因为您甚至在创建TextView实例之前就试图获取它

这四行应该在
onCreate()的
DisplayActivity
方法中:

TextView firstTextView = (TextView) findViewById(R.id.firstTextView);
TextView lastTextView = (TextView) findViewById(R.id.lastTextView);
TextView ageDisplayTextView = (TextView) findViewById(R.id.ageTextView);
TextView sexDisplayTextView = (TextView) findViewById(R.id.sexDisplayTextView); 

将findViewById()调用从成员变量初始化移动到setContentView()之后的onCreate()。在onCreate()之前,没有窗口,您将获得此NPE。在setContentView()之前,你找不到任何东西。

你能发布错误日志吗?我发布了错误日志作为编辑。嗯,我确实觉得自己像个傻瓜。这就解决了。谢谢你的帮助。