我对 Android 完全陌生,第一次尝试使用 Fragments。我使用了我在片段中转换的 ListView 活动。但不知何故,由 findViewById() 检索到的 ListView 元素给了我 null。谁能帮我解决这个问题。?
这是 ListView Fragment 的代码,在调用 setAdapter 方法后出现错误。日志值显示 timeTrackerListView 为 null。我不知道为什么。!!:(
package ray.kaushik.nasaapp;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class TimeTracker extends Fragment{
private Time_Tracker_Adapter timeTrackerAdapter;
private String TAG = "TimeTracker";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
timeTrackerAdapter = new Time_Tracker_Adapter();
}
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.time__tracker_layout, container, false);
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ListView timeTrackerListView = (ListView) getActivity().findViewById(R.id.time_tracker_List);
Log.i(TAG, "timeTrackerListView-->" + timeTrackerListView);
timeTrackerListView.setAdapter(timeTrackerAdapter);
}
}