简体   繁体   English

片段数组中“this”的第一个参数类型错误

[英]Wrong 1st Argument Type Error on "this" in Fragment Array

I had almost completed my app, and was doing the Navigation Menu last when I realized that you must use fragments instead of activities to have the same Navigation Menu throughout all activities.我几乎完成了我的应用程序,当我意识到必须使用片段而不是活动来在所有活动中拥有相同的导航菜单时,我正在做导航菜单。 So now, I am currently in the process of copying, pasting, and making activity java work in fragment java.所以现在,我目前正在复制、粘贴和使活动 java 在片段 java 中工作。 On my settings page I have a spinner that allows you to select a language.在我的设置页面上,我有一个允许您选择语言的微调器。 However, part of the code has an error in it that I can't seem to figure out.但是,部分代码有一个错误,我似乎无法弄清楚。 All help is very much appreciated!!非常感谢所有帮助! Thank You!谢谢你!

 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); } }

You need a Context for creating the ArrayAdapter from the resources.您需要一个上下文来从资源创建 ArrayAdapter。 The Fragment class does not have it's own context, rather it depends on the Activity in which it is hosted. Fragment 类没有自己的上下文,而是取决于托管它的 Activity。 So you need to pass in the context from the Activity in which your Fragment resides.因此,您需要从 Fragment 所在的 Activity 传入上下文。

This should have you sorted,这应该让你排序,

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

Hope this helps, Happin coding!希望这会有所帮助,Happin 编码!

Posting this other answer to answer your spinner not changing the language issue.发布这个其他答案来回答您的微调器而不改变语言问题。

In short you haven't told the Spinner where to go when an item is selected.简而言之,您还没有告诉 Spinner 在选择项目时要去哪里。 You have written the code to handle it but you haven't "linked" it to your Spinner.您已经编写了处理它的代码,但还没有将它“链接”到您的 Spinner。

First change this line at the beginning of your class,首先在课程开始时更改此行,

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

You will need this import at the beginning of the file just in case Android Studio does not auto-import,您将需要在文件开头进行此导入,以防 Android Studio 不自动导入,

import android.widget.AdapterView.OnItemSelectedListener;

Next you have to add an annotation to the onItemSelected method like this,接下来,您必须像这样向 onItemSelected 方法添加注释,

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

These two things help with allowing/letting the Fragment handle what to do when an item is clicked.这两件事有助于允许/让 Fragment 处理单击项目时要执行的操作。 Last we need to tell the Spinner that clicks will be taken care of by the Fragment.最后,我们需要告诉 Spinner 点击将由 Fragment 处理。

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

This helps the Spinner to "delegate" the handling of item selections to the Fragment.这有助于 Spinner 将项目选择的处理“委托”给 Fragment。

Hope this helps.希望这可以帮助。 Happy coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM