Hi,
Ich versuche eine Listview in einem Fragment zu benutzen. Aber getActivity ist null.
Hier ist mein Code:
ListView.java
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);
}
}
Alles anzeigen
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);
}
}
Alles anzeigen
Java
case 2:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new ListFragment())
.commit();
break;
Hat jmd vlt eine Idee?
mfg ternes3