代码之家  ›  专栏  ›  技术社区  ›  user7624209

Bundle在第一次运行时不为null

  •  0
  • user7624209  · 技术社区  · 8 年前

    我在MainActivity上创建了一个包

    和条件if(bundle!=Null)

    但在第一次运行中,程序为n null,程序关闭。我的代码:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_class);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
    
            Log.d("tag", "onCreate0: ");
            Width = Double.parseDouble(bundle.getString("Width"));
            height = Double.parseDouble(bundle.getString("height"));
    
        }
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   user7624209 user7624209    8 年前

    我自己解决了

     Intent intent11 = new Intent(context, MainClass.class);
                    int strName = -1;
                    intent11.putExtra("STRING_I_NEED", strName);
                    intent11.putExtra("Width", Width);
                    intent11.putExtra("height", height);
                    intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent11);
    
    
      Bundle bundle = getIntent().getExtras();
    
        if (bundle != null) {
    
            if (bundle.getInt("STRING_I_NEED") == -1) {
                Width = Double.parseDouble(bundle.getString("Width"));
                height = Double.parseDouble(bundle.getString("height"));
            }
        }