android - getContext().getSystemService 错误

标签 android android-context

所以我现在只是试图在我的 getView 函数中膨胀一个 View ,而 getContext() 出于某种原因说它是未定义的..

package com.MTSUAndroid;

import com.MTSUAndroid.Alarm_Settings.EfficientAdapter1.ViewHolder;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;

public class Alarm_Settings extends ListActivity {
    public static class EfficientAdapter1 extends BaseAdapter{
        private LayoutInflater mInflater;

        public EfficientAdapter1(Context context){
            mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            int viewType = this.getItemViewType(position);

            switch (viewType)
            {
            case 1:
                holder = new ViewHolder();

                View v = convertView;
                if (v == null)
                {
                    LayoutInflater vi = null;
                    //LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.alerts,parent,false);

                    holder.text1 = (TextView)v.findViewById(R.id.menu_Cancel);
                    v.setTag(holder);
                }
                else {
                    holder = (ViewHolder)v.getTag();
                }
                return v;
            case 2:
                ViewHolder holder1 = new ViewHolder();

                View v1 = convertView;
                if (v1 == null)
                {
                    LayoutInflater vi = null;
                    //LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v1 = vi.inflate(R.layout.about, parent, false);

                    holder1.text1 = (TextView)v1.findViewById(R.id.menu_Cancel);
                    v1.setTag(holder1);
                }
                else {
                    holder1 = (ViewHolder)v1.getTag();
                }

                return v1;
            }
            return null;
        }

        static class ViewHolder{
            TextView text1;
        }

    }
    public void onCreate(Bundle SavedInstanceState)
    {
        super.onCreate(SavedInstanceState);
        setListAdapter(new EfficientAdapter1(this));
        ListView listview = getListView();
        listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent intent = new Intent(Alarm_Settings.this, Alerts.class);
                startActivity(intent);
            }
        });
    }
}

这是我遇到问题的一段代码。 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); getContext() 没有为类定义,我不明白为什么 :(

最佳答案

您在类 EfficientAdapter1 中调用它,它不扩展 Activity 并且没有这样的方法。

在内部类中添加一个Context字段并调用getSystemService:

/* snip */
public static class EfficientAdapter1 extends BaseAdapter{
    private LayoutInflater mInflater;
    private Context ctx;
    public EfficientAdapter1(Context context){
        mInflater = LayoutInflater.from(context);
        ctx = context;
    }
/* snip */
     LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/* snip */

关于android - getContext().getSystemService 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6682201/

相关文章:

android - ViewPager 可以在每页中有多个 View 吗?

java - 设置 TextView 数组的上下文

android - 在 Toast .maketext() 方法中使用 getApplicationcontext() 和 this 有什么区别

android - ExpandableListView中的SwipeListView,可以吗?

Android布局: left item in horizontal LinearLayout forces right justified text to jumble

android - 生成足够大的列表时出现 ANR(强制关闭/等待)

android - 来自不同上下文的 StartActivity

android - getSupportFragmentManager() - 类型 Context 未定义

android - AndEngine - 超空间类型效果