错误的第一个参数类型错误"这个"在片段数组中

时间:2017-02-05 04:08:03

标签: java android android-studio android-fragments android-spinner

我几乎已经完成了我的应用程序,当我意识到你必须使用片段而不是活动来在所有活动中使用相同的导航菜单时,我最后都在进行导航菜单。所以现在,我目前正在片段java中复制,粘贴和制作活动java工作。在我的设置页面上,我有一个允许您选择语言的微调器。但是,部分代码中存在错误,我似乎无法弄清楚。非常感谢所有帮助!!谢谢!



package com.ezeapplications.quikflipfinal;


import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Locale;
import java.util.Set;


/**
 * A simple {@link Fragment} subclass.
 */
public class SettingsFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    public SettingsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        Button settupdatebtn = (Button) view.findViewById(R.id.setting_update_btn);
        settupdatebtn.setOnClickListener(this);

        Spinner langspinner = (Spinner) view.findViewById(R.id.settings_language_spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.lang_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        langspinner.setAdapter(adapter);
        return view;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        Spinner langspinner = (Spinner) view.findViewById(R.id.settings_language_spinner);
        langspinner.setOnItemSelectedListener(this);

        if (pos == 1) {

            Toast.makeText(parent.getContext(),
                    "You Have Selected English!", Toast.LENGTH_SHORT)
                    .show();
            setLocale("en");
            SettingsFragment fragmenten = new SettingsFragment();
            android.support.v4.app.FragmentTransaction fragmentTransactionen =
                    getActivity().getSupportFragmentManager().beginTransaction();
            fragmentTransactionen.replace(R.id.fragment_container, fragmenten);
            fragmentTransactionen.commit();
            langspinner.setSelection(1);

        } else if (pos == 2) {

            Toast.makeText(parent.getContext(),
                    "Has Seleccionado Español!", Toast.LENGTH_SHORT)
                    .show();
            setLocale("es");
            SettingsFragment fragmentes = new SettingsFragment();
            android.support.v4.app.FragmentTransaction fragmentTransactiones =
                    getActivity().getSupportFragmentManager().beginTransaction();
            fragmentTransactiones.replace(R.id.fragment_container, fragmentes);
            fragmentTransactiones.commit();
            langspinner.setSelection(2);

        } else if (pos == 3) {

            Toast.makeText(parent.getContext(),
                    "Vous Avez Sélectionné Le Français!", Toast.LENGTH_SHORT)
                    .show();
            setLocale("fr");
            SettingsFragment fragmentfr = new SettingsFragment();
            android.support.v4.app.FragmentTransaction fragmentTransactionfr =
                    getActivity().getSupportFragmentManager().beginTransaction();
            fragmentTransactionfr.replace(R.id.fragment_container, fragmentfr);
            fragmentTransactionfr.commit();
            langspinner.setSelection(3);
        }
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }


        @Override
                public void onClick (View v) {
            SettingsFragment fragment = new SettingsFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container,fragment);
            fragmentTransaction.commit();
            Toast.makeText(getActivity(), "Settings Updated!", Toast.LENGTH_SHORT).show();
        };

public void setLocale(String lang) {

        Locale myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        }

    }
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您需要一个Context来从资源创建ArrayAdapter。 Fragment类没有自己的上下文,而是取决于托管它的Activity。因此,您需要从Fragment所在的Activity中传入上下文。

这应该让你排序,

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.lang_array, android.R.layout.simple_spinner_item);

希望这有帮助,Happin编码!

答案 1 :(得分:0)

发布另一个答案来回答您的微调器,而不是改变语言问题。

简而言之,当选择一个项目时,你没有告诉Spinner去哪里。您已经编写了代码来处理它,但您没有将它“链接”到您的Spinner。

首先在课程开头更改此行,

public class SettingsFragment extends Fragment implements View.OnClickListener,
OnItemSelectedListener{...

为了防止Android Studio自动导入,您需要在文件开头进行此导入,

import android.widget.AdapterView.OnItemSelectedListener;

接下来,您必须向onItemSelected方法添加注释,如下所示,

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {...

这两件事有助于允许/让Fragment处理单击项目时要执行的操作。最后,我们需要告诉Spinner点击将由片段处理。

Spinner langspinner = (Spinner) view.findViewById(R.id.settings_language_spinner);
langspinner.setOnItemSelectedListener(this);

这有助于Spinner将项目选择的处理“委托”给片段。

希望这会有所帮助。快乐的编码!