我正在尝试ListView
在片段中使用 a 。但getActivity()
为空。我已经搜索并找到了很多关于这个主题的主题,但这些主题并不是很有帮助。
这是我的代码:
ListView.java
:
package de.listview.example;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class ListViews extends ListFragment {
private AdapterView.OnItemClickListener listener;
public void makelist()
{
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
MainActivity.java
:
public static class ListFragment extends Fragment
{
Context mContext;
@Override
public void onAttach(Activity activity) {
mContext = getActivity();
Log.i("Event", "onAttach called");
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ListViews lst = new ListViews();
lst.makelist();
return inflater.inflate(R.layout.whitelist_content, container, false);
}
}
case 2:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new ListFragment())
.commit();
break;