无法进行片段对片段的通信

时间:2020-03-19 00:07:25

标签: android android-fragments spinner android-spinner

我的目标是要使两个片段从上到下占据整个屏幕的一半。上半部分应使用一个片段来显示微调器。下半部分应使用片段将背景从下拉菜单更改为选定的微调框颜色。

我有一个用PaletteFragment制作的接口,并在PaletteActivity中实现了它。然后,我创建了一个名为sharedPosition的公共变量,该变量共享在PaletteFragmentCanvasFragment中选择的微调器项目的位置。 CanvasFragment扩展了PaletteActivity,因此我可以收到sharedPosition来适当调整片段的颜色。

但是,一旦我运行程序,顶部的片段就会显示出来,并显示微调器,但是一旦我从微调器中选择了一种颜色,应用就会崩溃。 Logcat显示sharedPoisition引起了该错误。我不确定为什么文件本身不抱怨。

enter image description here

到目前为止,这是我的项目外观,一旦我单击一种颜色,我希望它可以将屏幕下半部分的背景更改为该颜色。

PaletteFragment

package edu.temple.coloractivity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import java.util.Locale;



public class PaletteFragment extends Fragment {

    OnColorSelectListener callback;
    public void setOnColorSelectListener(OnColorSelectListener callback){
        this.callback = callback;
    };

    public interface OnColorSelectListener{
        public int onColorSelected(int position);
    }




    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public static PaletteFragment newIntance(){
        return new PaletteFragment();
    }
    public PaletteFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PaletteFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PaletteFragment newInstance(String param1, String param2) {
        PaletteFragment fragment = new PaletteFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_palette, container, false);
        Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                getActivity(), R.array.myColors, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> stringNames = ArrayAdapter.createFromResource(getActivity(), R.array.myStrings, android.R.layout.simple_spinner_item);
        stringNames.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> stringName = ArrayAdapter.createFromResource(getActivity(),R.array.myStrings, android.R.layout.simple_spinner_item);
        stringName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        View v = inflater.inflate(R.layout.fragment_palette,container,false);
        spinner = v.findViewById(R.id.spinner);
        spinner.setAdapter(new ColorAdapter(getActivity()));

        spinner = v.findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0){
                }
                else if(position == 1){
                    callback.onColorSelected(position);
                }else if(position == 2){
                    callback.onColorSelected(position);
                }else if(position == 3){
                    callback.onColorSelected(position);
                }else if(position == 4){
                    callback.onColorSelected(position);
                }else if(position == 5){
                    callback.onColorSelected(position);
                }else if(position == 6){
                    callback.onColorSelected(position);
                }else if(position == 7){
                    callback.onColorSelected(position);
                }else if(position == 8){
                    callback.onColorSelected(position);
                }else if(position == 9){
                    callback.onColorSelected(position);
                }else if(position == 10){
                    callback.onColorSelected(position);
                }

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        return v;
    }
//
}

PaletteActivity

package edu.temple.coloractivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.Locale;


public class PaletteActivity extends AppCompatActivity implements PaletteFragment.OnColorSelectListener{
    public int sharedPosition;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void onAttachFragment(Fragment fragment){

    }


    public int onColorSelected(int position) {
        sharedPosition = position;
        return position;
    }
}

CanvasFragment

package edu.temple.coloractivity;

import android.app.Activity;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import java.util.Locale;


public class CanvasFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_main, container, false);
        int pos = ((PaletteActivity)getActivity()).sharedPosition;
        String CurrentLang = Locale.getDefault().getLanguage();
        TextView text = v.findViewById(R.id.newColor);
        final FrameLayout newBackground = v.findViewById(R.id.cfrag_container);


        System.out.println(Locale.getDefault().getLanguage());
        if(pos == 1){

            newBackground.setBackgroundResource(R.color.silver);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 2){
            newBackground.setBackgroundResource(R.color.pink);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 3){
            newBackground.setBackgroundResource(R.color.red);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 4){
            newBackground.setBackgroundResource(R.color.orange);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 5){
            newBackground.setBackgroundResource(R.color.yellow);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 6){
            newBackground.setBackgroundResource(R.color.green);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 7){
            newBackground.setBackgroundResource(R.color.blue);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 8){
            newBackground.setBackgroundResource(R.color.indigo);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 9){
            newBackground.setBackgroundResource(R.color.violet);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }else if(pos == 10){
            newBackground.setBackgroundResource(R.color.brown);
            if(CurrentLang=="es"){
                displayColorName(newBackground,pos,text);
            }else{
                displayColorName(newBackground,pos,text);
            }
        }

        return v;
    }
    private void displayColorName(View newBackground, int pos, TextView text){
        String retrieve[] = getResources().getStringArray(R.array.myStrings);
        String color = retrieve[pos];
        text.setText(color);
    }
}

CanvasActivity

package edu.temple.coloractivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

public class CanvasActivity extends AppCompatActivity {

}

ColorAdapter

package edu.temple.coloractivity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

public class ColorAdapter extends BaseAdapter {
    ArrayList<Integer> colors;
    Context context;

    public ColorAdapter(Context context){

        this.context=context;
        colors = new ArrayList<Integer>();
        int retrieve []=context.getResources().getIntArray(R.array.myColors);
        for(int i:retrieve)
        {
            colors.add(i);
        }
    }
    @Override
    public int getCount() {
        return colors.size();
    }

    @Override
    public Object getItem(int args) {
        return colors.get(args);
    }

    @Override
    public long getItemId(int args) {
        return args;
    }

    public String getElementFromColors(int position){
        String CurrentLang = Locale.getDefault().getLanguage();
        if(CurrentLang.equals("es")){
            String retrieve[] = context.getResources().getStringArray(R.array.myStrings);
            return retrieve[position];
        }else{

            String retrieve[] = context.getResources().getStringArray(R.array.myStrings);
            return retrieve[position];
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=LayoutInflater.from(context);
        convertView=inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
        TextView txv=(TextView)convertView.findViewById(android.R.id.text1);
        txv.setBackgroundColor(colors.get(position));
        txv.setText(getElementFromColors(position));
        return convertView;
    }

}

0 个答案:

没有答案