0

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

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());

我会得到这个例外

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 类有问题,但是什么......以及为什么在简单的活动中一切正常......

我发现了这个问题并尝试使用代码但仍然没有

4

1 回答 1

1

好的,这里的问题是

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

  2. 您的空指针出现是因为

    扎孔菜单

在您创建适配器时尚未定义,而是在您的适配器之前创建这些元素,如下所示:

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 将被初始化。

于 2012-12-27T01:27:01.083 回答