2014-11-17 30 views
-1

我想在一個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); 
    } 
    } 

    } 
+0

MainActivity.this你不能在這個片段類使用getActivity改用而()創建適配器。並嘗試在時間解決一個問題 – virendrao

+0

您是否檢查過以下任何答案? – Piyush

回答

0

試圖改變這樣的代碼:

mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom,Languages)); 

有了這個:

mySpinner.setAdapter(new MyAdapter(getActivity(), R.layout.custom,Languages)); 

注意:使用getActivity()中的活動上下文而不是片段中的靜態。

+0

有沒有必要改變這一點。這不會影響。 – Piyush

1

這裏需要

mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom, 
     Languages)); 

改變

mySpinner.setAdapter(new MyAdapter(getActivity(), R.layout.custom, 
     Languages)); 

您使用的片段所以ContextgetActivity()

編輯:

在適配器類如果在充氣自定義視圖那麼這行也需要改變你是不是通過你的上下文的參考。

LayoutInflater inflater = getLayoutInflater(); 

LayoutInflater inflater = getActivity().getLayoutInflater(); 
+0

第一個解決方案不工作。它給出的錯誤構造函數GamesFragment.MyAdapter(Context,int,String [])引用缺少的類型上下文) – divya

+0

@divya你需要改變那裏! – Piyush

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> 

+0

在發生實際錯誤的地方添加必要的代碼。無需在這裏發佈xml。 – Piyush