String Extra不传递给新Activity

时间:2015-09-15 02:53:07

标签: android android-intent android-activity

我试图通过Intent将String extra传递给一个活动。我传递了一个布尔值和一个字符串。布尔传递得很好,我已经通过打印出来在我的logcat中确认了它。但是,String一直保持为null。

以下是我在TopicsActivity.java中传递intent的代码:

private class TopicsListListener implements AdapterView.OnItemClickListener {

    /*
     * Fields for the keys and topic tree map
     */
    private final String[] keys;
    private final Map topicTree;

    /*
     * Constructor accepts key array and topic tree map and assigns them to respective fields
     */
    TopicsListListener(String[] keys, Map topicTree) {
        super();
        this.topicTree = topicTree;
        this.keys = keys;
    }

    /*
     * Passes an Intent to the Public Messages List activity containing the t-[id] of the
     * selected topic and an isPrivate value of false (Public)
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent topicMessages = new Intent(getBaseContext(), MessagesActivity.class);
        String TID = (String) topicTree.get(keys[position]);
        Log.d("TID", TID);
        Bundle intentBundle = new Bundle();
        intentBundle.putBoolean("isPrivate", isPrivate);
        intentBundle.putString("tid", TID);
        topicMessages.putExtra("bundle", intentBundle);
        startActivity(topicMessages);

    }
}

这是我的代码,我试图检索字符串:

/*
 * Fields for the Activity
 */
private ListView topicsList;
public boolean isPrivate;

/*
 * Fields used for Intents
 */
private String TID;

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

    /*
     * Get the status of isPrivate from the Dashboard activity
     */
    Intent topics = getIntent();
    Bundle intentInfo = topics.getBundleExtra("bundle");
    isPrivate = intentInfo.getBoolean("isPrivate");
    Log.d("TOPICS isPrivate", "" + isPrivate);
    TID = intentInfo.getString("tid");

    TextView temp = (TextView)findViewById(R.id.tempText);
    temp.setText(TID);

temp.setText(TID); line正在抛出NullPointerException。当发生这种情况时,这是我的一块logcat:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.topicplaces.android.MessagesActivity.onCreate(MessagesActivity.java:66)
        at android.app.Activity.performCreate(Activity.java:6221)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)

以下是xml中的TextView

 <TextView android:id="@+id/tempText" 
     android:layout_width="wrap_content"    
     android:layout_height="wrap_content" android:textSize="20sp"      
     android:textColor="#FFFFFF" android:text="Hello World" 
     android:layout_above="@+id/newMessageButton" 
     android:layout_centerHorizontal="true"/>

如果重要,还可以通过另一个名为Dashboard.java的活动的Intent启动TopicsActivity.java。感谢所有的帮助,我一整天都在摸不着头脑。我想我已经在StackOverflow上阅读了每篇Intent相关的帖子,但没有找到任何解释。

4 个答案:

答案 0 :(得分:1)

由于某种原因,你的 TextView 等于null,而不是 TID 字符串
可以在任何 TextView 上使用setText(null),它不会让您的应用崩溃,只需清除文字
您确定活动activity_topics.xml布局中的 TextView 带有 tempText id属性吗?

android:id="@+id/tempText"

答案 1 :(得分:0)

试试这个 -

private class TopicsListListener implements AdapterView.OnItemClickListener {

    /*
     * Fields for the keys and topic tree map
     */
    private final String[] keys;
    private final Map topicTree;
    private Context mContext;

    /*
     * Constructor accepts key array and topic tree map and assigns them to respective fields
     */
    TopicsListListener(Context context, String[] keys, Map topicTree) {
        super();
        this.topicTree = topicTree;
        this.keys = keys;
        mContext = context;
    }

    /*
     * Passes an Intent to the Public Messages List activity containing the t-[id] of the
     * selected topic and an isPrivate value of false (Public)
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent topicMessages = new Intent(mContext, MessagesActivity.class);
        String TID = (String) topicTree.get(keys[position]);
        topicMessages.putExtra("TID", TID);
        topicMessages.putExtra("isPrivate", isPrivate);
        mContext.startActivity(topicMessages);    
    }
}

它应该可以解决您的问题。

答案 2 :(得分:0)

尝试,更改:

topicMessages.putExtra("bundle", intentBundle);
Bundle intentInfo = topics.getBundleExtra("bundle");

要:

topicMessages.putExtra(intentBundle);
Bundle intentInfo = topics.getExtra();

答案 3 :(得分:0)

尝试更改接收捆绑包的代码,如:

Bundle intentInfo = topics.getBundleExtra("bundle");

topicMessages.putExtra("bundle", intentBundle);

Bundle intentInfo = topics.getExtras("bundle");

topicMessages.putExtras("bundle", intentBundle);

我认为你也没有得到布尔值,可能是你在追逐 false,它是布尔变量的默认值。