我正在开发一个包含2个标签的应用程序。
在两个标签上都有一个ListView
,而且我使用两个标签的自定义适配器。
我知道,为了获得context
标签,我应该使用getActivity()
它适用于第一个选项卡,但它不适用于第二个选项卡。
以下是自定义adapter
代码:
public class DownloadedAdapter extends BaseAdapter {
public static ArrayList<Downloads_item> DownloadedList;
Context context;
Typeface face;
LayoutInflater inflater;
public DownloadedAdapter(Context cont, ArrayList<Downloads_item> ls) {
cont = context;
DownloadedList=ls;
//face = Typeface.createFromAsset(cont.getAssets(), "font.ttf");
}
@Override
public int getCount() {
return DownloadedList.size();
}
@Override
public Object getItem(int position) {
return DownloadedList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.download_item, parent, false);
Downloads_item current_item = DownloadedList.get(position);
TextView txt_download_item_name = (TextView) itemView
.findViewById(R.id.txt_downloads_item_name);
TextView txt_download_item_date = (TextView) itemView
.findViewById(R.id.txt_downloads_item_date);
txt_download_item_name.setText(current_item.get_sora()
.get_arabic_name());
txt_download_item_date
.setText("\u062A\u0645 \u0627\u0644\u062A\u062D\u0645\u064A\u0644 \u0641\u064A: "
+ current_item.get_date());
txt_download_item_name.setTypeface(face);
return itemView;
}
}
这是第二个标签代码:
public class DownloadedListTab extends SherlockFragment {
ListView list_downloads_list;
public static ArrayList<Downloads_item> array_list;
public static ArrayList<Downloads_item> copy_array_list;
Database database_obj;
Context context;
DownloadedAdapter adapter;
TextView txt_search_name;
EditText edit_search_value;
TextView txt_no_results;
LayoutInflater inflater;
Typeface face;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.downloadedtab,container,false);
prepare_array_list();
list_downloads_list = (ListView) view.findViewById(R.id.list_downloads_list);
txt_no_results = (TextView) view.findViewById(R.id.txt_no_results);
database_obj = new Database(getActivity());
adapter = new DownloadedAdapter(getActivity(),copy_array_list);
list_downloads_list.setAdapter(adapter);
if (copy_array_list.size() == 0) {
txt_no_results
.setText("\u0644\u0645 \u064A\u062A\u0645 \u062A\u062D\u0645\u064A\u0644 \u0627\u064A \u0633\u0648\u0631\u0629 \u062D\u062A\u064A \u0627\u0644\u0623\u0646");
txt_no_results.setVisibility(View.VISIBLE);
txt_no_results.setTypeface(face);
txt_no_results.setTextSize(30);
txt_no_results.setTextColor(Color.parseColor("#878787"));
list_downloads_list.setVisibility(View.INVISIBLE);
} else {
// adapter.notifyDataSetChanged();
txt_no_results.setVisibility(View.INVISIBLE);
list_downloads_list.setVisibility(View.VISIBLE);
}
return view;
}
private void prepare_array_list() {
array_list = new ArrayList<Downloads_item>();
copy_array_list = new ArrayList<Downloads_item>();
TreeMap<String, String> temp = new TreeMap<String, String>();
temp = database_obj.get_all_sower();
for (String key : temp.keySet()) {
array_list.add(new Downloads_item(Splash_1.Data_Tree.Tree_of_Soras
.get(key), temp.get(key)));
copy_array_list.add(new Downloads_item(
Splash_1.Data_Tree.Tree_of_Soras.get(key), temp.get(key)));
}
}
}
我知道问题在于getActivity的上下文。我尝试了getApplicationContext
,getBaseContext
,getApplication
,但没有任何作用
我知道如何解决这个问题?
database_obj = new Database(getActivity());
adapter = new DownloadedAdapter(getActivity(), copy_array_list);
logcat的
08-13 12:08:06.772: E/AndroidRuntime(1974): FATAL EXCEPTION: main
08-13 12:08:06.772: E/AndroidRuntime(1974): Process: com.idroid.elsidees, PID: 1974
08-13 12:08:06.772: E/AndroidRuntime(1974): java.lang.NullPointerException
08-13 12:08:06.772: E/AndroidRuntime(1974): at com.idroid.elsidees.DownloadedListTab.prepare_array_list(DownloadedListTab.java:73)
08-13 12:08:06.772: E/AndroidRuntime(1974): at com.idroid.elsidees.DownloadedListTab.onCreateView(DownloadedListTab.java:44)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.os.Handler.handleCallback(Handler.java:733)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.os.Handler.dispatchMessage(Handler.java:95)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.os.Looper.loop(Looper.java:136)
08-13 12:08:06.772: E/AndroidRuntime(1974): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-13 12:08:06.772: E/AndroidRuntime(1974): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 12:08:06.772: E/AndroidRuntime(1974): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 12:08:06.772: E/AndroidRuntime(1974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-13 12:08:06.772: E/AndroidRuntime(1974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-13 12:08:06.772: E/AndroidRuntime(1974): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
尝试在结构函数中反转上下文赋值
public DownloadedAdapter(Context cont, ArrayList < Downloads_item > ls) {
cont = context; //<---- no correct
DownloadedList = ls;
//face = Typeface.createFromAsset(cont.getAssets(), "font.ttf");
}
在
public DownloadedAdapter(Context cont, ArrayList < Downloads_item > ls) {
this.context = cont; //<--- this is correct.
DownloadedList = ls;
//face = Typeface.createFromAsset(cont.getAssets(), "font.ttf");
}
端
private void prepare_array_list() {
...
temp = database_obj.get_all_sower(); // <---- database_obj is NULL
...
}
在调用prepare_array_list()
之前初始化数据库objdatabase_obj = new Database(getActivity());
prepare_array_list();