我想在一個fragment中製作一個自定義微調框。但是我無法做到這點。創建spinner時遇到以下錯誤。請幫助我解決這些錯誤。值得注意的是在片段中創建微調框
的錯誤,在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);
}
}
}
MainActivity.this你不能在這個片段類使用getActivity改用而()創建適配器。並嘗試在時間解決一個問題 – virendrao
您是否檢查過以下任何答案? – Piyush