我正在尝试在片段中创建自定义微调器。但是我无法创建它。创建spinner时会遇到以下错误.Plese帮助我解决这些错误。
java文件中的错误:
1.The method getLayoutInflater(Bundle) in the type Fragment is not applicable for the arguments () GamesFragment.java /dailyexpenses/src/com/example/dailyexpenses line 47 Java Problem
2.No enclosing instance of the type MainActivity is accessible in scope GamesFragment.java /dailyexpenses/src/com/example/dailyexpenses line 31 Java Problem
3.The constructor GamesFragment.MyAdapter(Context, int, String[]) refers to the missing type Context GamesFragment.java /dailyexpenses/src/com/example/dailyexpenses line 31 Java Problem
4.Context cannot be resolved to a type GamesFragment.java /dailyexpenses/src/com/example/dailyexpenses line 38 Java Problem
这是我的java文件
package com.example.dailyexpenses;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class GamesFragment extends Fragment {
String[] Languages = { "Select a Language", "C# Language", "HTML Language",
"XML Language", "PHP Language" };
// Declaring the Integer Array with resourse Id's of Images for the Spinners
Integer[] images = { 0, R.drawable.icon, R.drawable.petrol,
R.drawable.books, R.drawable.recharge};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
Spinner mySpinner = (Spinner) rootView.findViewById(R.id.spinner1);
mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom,
Languages));
return rootView;
}
public class MyAdapter extends ArrayAdapter {
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
// Inflating the layout for the custom Spinner
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom, parent, false);
// Declaring and Typecasting the textview in the inflated layout
TextView tvLanguage = (TextView) layout
.findViewById(R.id.tvLanguage);
// Setting the text using the array
tvLanguage.setText(Languages[position]);
// Setting the color of the text
// Declaring and Typecasting the imageView in the inflated layout
ImageView img = (ImageView) layout.findViewById(R.id.imgLanguage);
// Setting an image using the id's in the array
img.setImageResource(images[position]);
// Setting Special atrributes for 1st element
if (position == 0) {
// Removing the image view
img.setVisibility(View.GONE);
// Setting the size of the text
tvLanguage.setTextSize(20f);
// Setting the text Color
}
return layout;
}
// It gets a View that displays in the drop down popup the data at the specified position
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
// It gets a View that displays the data at the specified position
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
}
}
答案 0 :(得分:1)
你需要在这里改变
mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom,
Languages));
到
mySpinner.setAdapter(new MyAdapter(getActivity(), R.layout.custom,
Languages));
您正在使用片段,因此Context
将为getActivity()
。
<强> 编辑: 强>
在您的Adapter类中,如果您在膨胀自定义视图时未传递上下文的引用,则此行也需要更改。
LayoutInflater inflater = getLayoutInflater();
到
LayoutInflater inflater = getActivity().getLayoutInflater();
答案 1 :(得分:0)
尝试更改此代码:
mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom,Languages));
有了这个:
mySpinner.setAdapter(new MyAdapter(getActivity(), R.layout.custom,Languages));
注意:使用getActivity()中的活动上下文而不是片段中的static。
答案 2 :(得分:0)
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class My_Fragment extends Fragment
{
Spinner spinner;
String[] Languages = { "Select a Language", "C# Language", "HTML Language",
"XML Language", "PHP Language" };
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.myfragment, null);
spinner = (Spinner) view.findViewById(R.id.spinner);
adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowspinner,R.id.textView1 ,Languages);
spinner.setAdapter(adapter);
return view;
}
}
xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#345678">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
row spnieer
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>