【问题标题】:Android: putExtras and IntentsAndroid:putExtras 和 Intents
【发布时间】:2013-06-14 08:16:17
【问题描述】:

我在将字符串从片段传递到另一个活动时遇到问题。 我尝试了许多使用意图传递它们的方法(例如普通的附加组件、捆绑包),但附加组件在活动中始终为空。

我看过

http://www.vogella.com/articles/AndroidIntent/ 但是没有变化 http://developer.android.com/training/basics/firstapp/starting-activity.html这种传数据的方法也不行

关于 stackoverflow 的其他类似问题 - 但这些并不完全相同

我要做的是获取在片段中的两个 EditTexts 中输入的文本,然后将该文本传递给两个 EditTexts 填充相同文本的活动。问题是活动中的两个 EditText 中没有出现任何内容。我知道片段中的 EditText 正在工作,因为可以使用它们创建通知。

我的代码:我删除了我认为不必要的内容,例如将片段添加到导航抽屉布局。请原谅缺少括号 - 我已经删除了很多代码,有些可能是不小心被删除的! :-)

这是我创建意图的片段:

// Package declaring and importing stuff

public class QuickNoteFragment extends Fragment implements OnClickListener { 

// Removed some stuff


    EditText body;
    EditText title;
    Button create;
    int counter = 0;

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


        body = (EditText) rootView.findViewById(R.id.qn_et_body);
        title = (EditText) rootView.findViewById(R.id.qn_et_title);

        create.setOnClickListener(this); 


        // Removed stuff

        getActivity().setTitle(noter_activity); // Part of navigation drawer?
        return rootView;
        }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub



         switch (v.getId()) { 

         case R.id.qn_b_create:

        String content_text = body.getText().toString();
        String content_title = title.getText().toString();

                if (content_title.length() >=1){

            Context context = v.getContext();

// This intent does not seem to work
            Intent eIntent = new Intent(context, QuickNoteEdit.class);
            eIntent.putExtra("eTitle", content_title);
            eIntent.putExtra("eText", content_text);
            PendingIntent EditPendingIntent = PendingIntent.getActivity(context, 0, eIntent, 0);

// This intent works comletely. This is called when a notification action button is pressed
            Intent qnCancel = new Intent();
            qnCancel.setAction("com.RiThBo.noter.qnCancelBroadcast");
            Bundle extras = new Bundle();
            extras.putInt("valueOfCounter", counter);  
            qnCancel.putExtras(extras);  
            startBroadcast(qnCancel);
            PendingIntent pQnCancel = PendingIntent.getBroadcast(this.getActivity(), 0, qnCancel, 0);

// Creates Notification


                } else { 
            // Do something
                }

         case R.id.*** // Does something else

         }
    }

    private void startBroadcast(Intent qnCancel) { // This is part of the correctly working intent
        // TODO Auto-generated method stub

    }

}

这是我想要获得额外奖励的活动

// Removed package and imports

public class QuickNoteEdit extends Activity implements OnClickListener {

    EditText body;
    EditText title;

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

        variableConnector(); // This gets the id for all the items in the xml

        Intent intent = getIntent();
        String gotTitle = intent.getStringExtra("content_title"); // This is where I think it equals null.  Because the 
            String gotBody = intent.getStringExtra("content_text");

        title.setText(gotTitle); 
        body.setText(gotBody);

        }

        private void variableConnector() {
            // TODO Auto-generated method stub

            body = (EditText) findViewById(R.id.qne_et_body);
            title = (EditText) findViewById(R.id.qne_et_title);

        }

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

    }


}

谢谢

【问题讨论】:

    标签: android android-intent bundle extras


    【解决方案1】:

    你说的是:

    eIntent.putExtra("eTitle", content_title);
    eIntent.putExtra("eText", content_text);
    

    当你阅读它们时:

    String gotTitle = intent.getStringExtra("content_title");
    String gotBody = intent.getStringExtra("content_text");
    

    您需要匹配键...输入“eTitle”并阅读“eTitle”,而不是“*content_title*”!

    【讨论】:

    • 谢谢!!!那对我来说非常愚蠢:-)我花了很长时间试图弄清楚..我不会再犯那个错误了!
    【解决方案2】:

    您正在使用“eTitle”和“eText”标记您的附加内容,并尝试使用“content_title”和“content_text”检索它们。

    切换到

    String gotTitle = intent.getStringExtra("eTitle");  
    String gotBody = intent.getStringExtra("eText");
    

    它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2022-12-14
      • 2020-07-31
      相关资源
      最近更新 更多