Android 将数据从片段传递到片段活动不起作用

Android 将数据从片段传递到片段活动不起作用,android,event-handling,fragment,Android,Event Handling,Fragment,我不确定我设置的代码为什么不能正常工作 我试图启动一个活动,并在调用意图打开该活动时传递一些数据 由于某些原因,这并没有按计划进行,当我单击我的列表项打开一个活动时,只会出现一个空白的黑屏,这不应该发生,而且我的数据传递也不正确 这是我的代码,希望有人能指引我正确的方向 呼叫代码: @Override public void onListItemClick(ListView l, View v, int position, long id) { if (position ==

我不确定我设置的代码为什么不能正常工作

我试图启动一个活动,并在调用意图打开该活动时传递一些数据

由于某些原因,这并没有按计划进行,当我单击我的列表项打开一个活动时,只会出现一个空白的黑屏,这不应该发生,而且我的数据传递也不正确

这是我的代码,希望有人能指引我正确的方向

呼叫代码:

    @Override
public void onListItemClick(ListView l, View v, int position, long id) {

    if (position == 0) {

        String msg = "First";
        Intent i = new Intent(getActivity(), ImageGridActivity.class);
        i.putExtra("1", msg);
        startActivity(i);
    }
    if (position == 1) {

        String msg = "Second";
        Intent i = new Intent(getActivity(), ImageGridActivity.class);
        i.putExtra("2", msg);
        startActivity(i);
    }

}
装卸类别:

public class ImageGridActivity extends FragmentActivity {

private static final String Abstract = "Abstract";
private static final String Animal = "Animal";


@Override
protected void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    String msg = extras.getString("KeyMessage");

    if (msg == "1") {

        if (getSupportFragmentManager().findFragmentByTag(Abstract) == null) {
            final FragmentTransaction ft = getSupportFragmentManager()
                    .beginTransaction();
            ft.add(android.R.id.content, new AbstractGridFragment(),
                    Abstract);
            ft.commit();

        }

        if (msg == "2") {

        }
        if (getSupportFragmentManager().findFragmentByTag(Animal) == null) {
            final FragmentTransaction ft = getSupportFragmentManager()
                    .beginTransaction();
            ft.add(android.R.id.content, new AnimalGridFragment(), Animal);
            ft.commit();
        }

    }

}
}
钥匙不匹配

    String msg = extras.getString("KeyMessage");
而你的钥匙是“1”和“2”

检查文件

public Intent putExtra(字符串名称、字符串值)

将扩展数据添加到意图中。名称必须包含软件包前缀,例如,应用程序com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称

参数

name    The name of the extra data, with package prefix.
value   The String data value.
返回

Returns the same Intent object, for chaining multiple calls into a single statement.
你可能想要

   Intent i = new Intent(getActivity(), ImageGridActivity.class);
   i.putExtra("key", "1"); // "key" is the key and "1" is the value
   startActivity(i);

然后

还可以使用
.equals
.equalsIgnoreCase
来比较字符串

   if (msg.equals("1")) {
钥匙不匹配

    String msg = extras.getString("KeyMessage");
而你的钥匙是“1”和“2”

检查文件

public Intent putExtra(字符串名称、字符串值)

将扩展数据添加到意图中。名称必须包含软件包前缀,例如,应用程序com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称

参数

name    The name of the extra data, with package prefix.
value   The String data value.
返回

Returns the same Intent object, for chaining multiple calls into a single statement.
你可能想要

   Intent i = new Intent(getActivity(), ImageGridActivity.class);
   i.putExtra("key", "1"); // "key" is the key and "1" is the value
   startActivity(i);

然后

还可以使用
.equals
.equalsIgnoreCase
来比较字符串

   if (msg.equals("1")) {
钥匙不匹配

    String msg = extras.getString("KeyMessage");
而你的钥匙是“1”和“2”

检查文件

public Intent putExtra(字符串名称、字符串值)

将扩展数据添加到意图中。名称必须包含软件包前缀,例如,应用程序com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称

参数

name    The name of the extra data, with package prefix.
value   The String data value.
返回

Returns the same Intent object, for chaining multiple calls into a single statement.
你可能想要

   Intent i = new Intent(getActivity(), ImageGridActivity.class);
   i.putExtra("key", "1"); // "key" is the key and "1" is the value
   startActivity(i);

然后

还可以使用
.equals
.equalsIgnoreCase
来比较字符串

   if (msg.equals("1")) {
钥匙不匹配

    String msg = extras.getString("KeyMessage");
而你的钥匙是“1”和“2”

检查文件

public Intent putExtra(字符串名称、字符串值)

将扩展数据添加到意图中。名称必须包含软件包前缀,例如,应用程序com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称

参数

name    The name of the extra data, with package prefix.
value   The String data value.
返回

Returns the same Intent object, for chaining multiple calls into a single statement.
你可能想要

   Intent i = new Intent(getActivity(), ImageGridActivity.class);
   i.putExtra("key", "1"); // "key" is the key and "1" is the value
   startActivity(i);

然后

还可以使用
.equals
.equalsIgnoreCase
来比较字符串

   if (msg.equals("1")) {

您的按键发送/接收错误。你应该这样做:

实例化字符串
msg

String msg;
如果

否则

msg = "2";
i.putExtra("myKey", msg);  
因此:


希望对您有所帮助。

您在发送/接收键时出错。你应该这样做:

实例化字符串
msg

String msg;
如果

否则

msg = "2";
i.putExtra("myKey", msg);  
因此:


希望对您有所帮助。

您在发送/接收键时出错。你应该这样做:

实例化字符串
msg

String msg;
如果

否则

msg = "2";
i.putExtra("myKey", msg);  
因此:


希望对您有所帮助。

您在发送/接收键时出错。你应该这样做:

实例化字符串
msg

String msg;
如果

否则

msg = "2";
i.putExtra("myKey", msg);  
因此:


希望有帮助。

碎片活动的
setContentView
在哪里?碎片活动的
setContentView
在哪里?碎片活动的
setContentView
在哪里?碎片活动的
setContentView
在哪里?