在单击按钮上更改片段的颜色

时间:2017-01-17 07:49:33

标签: android

我正在开发一个应用,其中主Activity有三个buttons红色,绿色和蓝色以及fragment。点击button fragment的颜色会更改button的相应颜色。我尝试更改background color onclick`的fragment,但我的应用崩溃了

public class MainActivity extends AppCompatActivity {
Button b1,b2,b3;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);
    b3 = (Button) findViewById(R.id.button3);

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            fragment1.rootView.setBackgroundColor(Color.RED);
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            fragment1.rootView.setBackgroundColor(Color.GREEN);
        }
    });
    b3.setOnClickListener(new View.OnClickListener(){
    public void onClick(View arg0) {
    fragment1.rootView.setBackgroundColor(Color.BLUE);
   }
  });
    }


    }

logcat的:

01-17 13:27:02.976 17105-17105/com.example.shivadeeps.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shivadeeps.myapplication, PID: 17105
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference
    at com.example.shivadeeps.myapplication.MainActivity$2.onClick(MainActivity.java:39)
    at android.view.View.performClick(View.java:5246)
    at android.widget.TextView.performClick(TextView.java:10565)
    at android.view.View$PerformClick.run(View.java:21200)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6946)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

片段

public class fragment1 extends Fragment {
  public static View rootView;

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment1, container, false);
    }
}

3 个答案:

答案 0 :(得分:0)

为简单起见,

在XML布局中将片段的android:background设置为#AARRGGBB格式的透明。即。

android:background="#00000000"

并且,在MainActivity上的onClick()事件中,只需更改包含片段的布局的背景颜色即可。即在MainActivity中

findViewById(R.id.root_layout).setBackgroundColor(Color.RED);

此处root_layout是MainActivity的顶级根级布局。您可以选择包含片段的布局,以便在所需位置生效。

在这种方法中,你没有对片段做任何事情。

答案 1 :(得分:0)

你可以找到逐个标签的片段并设置根视图的颜色

 Fragment fragmentManager = getFragmentManager().findFragmentByTag("fragment1");
        if (fragmentManager != null) {
            fragmentManager.getView().getRootView().setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        }

答案 2 :(得分:0)

检查这个答案:

package com.example.shivadeeps.myapplication;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import static android.R.attr.fragment;

public class fragment1 extends Fragment {
public static View rootView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
}