【问题标题】:Trying to pass a background color through intentPutExtra试图通过 intentPutExtra 传递背景颜色
【发布时间】:2017-03-29 16:02:55
【问题描述】:

正如标题所说,我正在尝试使用 intentPutExtra 设置背景颜色,但它似乎没有改变,这是我在 MainActivity 的意图:

  @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {

                    Intent intent = new Intent(MainActivity.this, ktmList.class);
                   intent.putExtra("backgroundColor", getResources().getColor(R.color.colorOrange));
                    startActivity(intent);
}

这就是我尝试更改背景颜色的方式:

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

    Intent intent = new Intent();
    int backgroundColor = intent.getIntExtra("backgroundColor", -1);

    LinearLayout rootView = (LinearLayout)findViewById(R.id.models_rootView);
    rootView.setBackgroundColor(backgroundColor);

这是 models_list xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/models_rootView"
    android:orientation="vertical">

<ListView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/models_list"
                android:padding="16dp"/>

</LinearLayout>

感谢任何形式的帮助!

【问题讨论】:

    标签: android listview android-intent


    【解决方案1】:

    使用rootView.setBackgroundResource(backgroundColor); 代替rootView.setBackgroundColor(backgroundColor);

    也改变

    intent.putExtra("backgroundColor", getResources().getColor(R.color.colorOrange));
    

    intent.putExtra("backgroundColor", R.color.colorOrange);
    

    这对我有用

    【讨论】:

    • 现在当我单击带有 java.lang.RuntimeException 的数组时我的应用程序崩溃:无法启动活动 ComponentInfo{com.ofir.motoinfo/com.ofir.motoinfo.brandlists.ktmList}:android.content .res.Resources$NotFoundException: 资源 ID #0xffffffff
    • 还是出现同样的错误,可能问题出在我的intent.getIntExtra("backgroundColor", -1);?第一次使用get extras,这个方法合适吗?
    • 糟糕。我错过了。不,你做错了。使用 getIntent().getIntExtra("backgroundColor", -1);
    猜你喜欢
    • 1970-01-01
    • 2022-06-23
    • 2020-02-27
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2022-10-23
    • 2023-02-13
    • 2021-09-30
    相关资源
    最近更新 更多