android片段定制适配器

时间:2012-12-27 00:59:12

标签: android adapter fragment

我在Android Fragment中使用自定义适配器时遇到一些问题。 这是代码。但在我在活动中使用此代码之前,一切都很好。在使用碎片时我无法找到错误的地方。

package com.testapp.test.fragments;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Fragment;
import com.testapp.test.R;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LawDocFragment extends Fragment{

private static LawDocFragment instance;

public static LawDocFragment getInstance() {
    if (LawDocFragment.instance == null) {
        return new LawDocFragment();
    }
    return LawDocFragment.instance;
}

public LawDocFragment() {
    LawDocFragment.instance = this;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

String[] zakonmenubig;
String[] zakonmenusmall;
Integer[] zakonmenuicon = { R.drawable.ic_menu_zakonu_pravila, 
                            R.drawable.ic_menu_zakonu_strafu, 
                            R.drawable.ic_menu_zakonu_doc,
                            R.drawable.ic_menu_zakonu_u77,
                            R.drawable.ic_menu_zakonu_u33,
                            R.drawable.ic_menu_zakonu_u400,
                            R.drawable.ic_menu_zakonu_p1103};

myAdapter zakonAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View LawDoc = inflater.inflate(R.layout.zakonbaza);     
    ListView zakonmenulist = (ListView)LawDoc.findViewById(R.id.zakonmenulist);     
    zakonAdapter = new myAdapter();     
    zakonmenulist.setAdapter(zakonAdapter);

    zakonmenubig = getResources().getStringArray(R.array.zakonmenubig);
    zakonmenusmall = getResources().getStringArray(R.array.zakonmenusmall);

    return LawDoc;
}

//start myAdapter
public class myAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;

    public myAdapter(Context ctx) {
        mLayoutInflater = LayoutInflater.from(ctx);
    }

    public int getCount() {
        return zakonmenubig.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public String getString(int position) {
        return zakonmenubig[position] + " (" + zakonmenusmall[position] + ")";
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = mLayoutInflater.inflate(R.layout.list_zakonbaza, null);

        ImageView image = (ImageView) convertView.findViewById(R.id.icon);
        image.setImageResource(zakonmenuicon[position]);

        TextView sign = (TextView) convertView.findViewById(R.id.zakonmenuitem);
        sign.setText(zakonmenubig[position]);

        TextView date = (TextView) convertView.findViewById(R.id.zakonmenuitemsmall);
        date.setText(zakonmenusmall[position]);
        return convertView;
    }
} // end myAdapter

}

现在Eclipse向我显示错误“构造函数LawDocFragment.myAdapter()未定义”在行

zakonAdapter = new myAdapter();

如果我要改为

zakonAdapter = new myAdapter(getActivity());

我会得到这个EXCEPTION

12-27 00:39:25.297: E/AndroidRuntime(652): FATAL EXCEPTION: main
12-27 00:39:25.297: E/AndroidRuntime(652): java.lang.NullPointerException
12-27 00:39:25.297: E/AndroidRuntime(652):  at com.testapp.test.fragments.fragments.LawDocFragment$myAdapter.getCount(LawDocFragment.java:72)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.widget.ListView.setAdapter(ListView.java:436)
12-27 00:39:25.297: E/AndroidRuntime(652):  at com.testapp.test.fragments.fragments.LawDocFragment.onCreateView(LawDocFragment.java:55)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.support.v4.app._HoloFragment.onCreateView(_HoloFragment.java:177)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.os.Handler.handleCallback(Handler.java:587)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.os.Looper.loop(Looper.java:123)
12-27 00:39:25.297: E/AndroidRuntime(652):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-27 00:39:25.297: E/AndroidRuntime(652):  at java.lang.reflect.Method.invokeNative(Native Method)
12-27 00:39:25.297: E/AndroidRuntime(652):  at java.lang.reflect.Method.invoke(Method.java:521)
12-27 00:39:25.297: E/AndroidRuntime(652):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-27 00:39:25.297: E/AndroidRuntime(652):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-27 00:39:25.297: E/AndroidRuntime(652):  at dalvik.system.NativeStart.main(Native Method)    

在行“LawDocFragment.java:72”中我有这段代码

return zakonmenubig.length;

并且符合“LawDocFragment.java:55”我有

zakonmenulist.setAdapter(zakonAdapter);

我认为类myAdapter有问题,但是......为什么在简单的活动中一切都很好..

我找到了这个issue并尝试使用代码,但仍然没有

1 个答案:

答案 0 :(得分:1)

好的问题是

  1. 让空白构造函数显示错误是正确的,因为在适配器类中没有类似的构造函数。所以要么创建一个没有参数的构造函数(坏主意),要么只是传入上下文。

  2. 出现空指针,因为

    zakonmenubig

  3. 在创建适配器时尚未定义

    ,而是在适配器之前创建这些元素,如下所示:

    zakonmenubig = getResources().getStringArray(R.array.zakonmenubig);
    zakonmenusmall = getResources().getStringArray(R.array.zakonmenusmall);
    
    ListView zakonmenulist = (ListView)LawDoc.findViewById(R.id.zakonmenulist);     
    zakonAdapter = new myAdapter();     
    zakonmenulist.setAdapter(zakonAdapter);
    

    现在你的zakonmenubig将被初始化。