我想让我的片段变得动态,所以我可以替换片段以进行更新。 但是现在我做了它我在我的应用程序中得到了nullpointerexception。
这是错误:
11-24 13:00:27.429: E/AndroidRuntime(31304): FATAL EXCEPTION: main
11-24 13:00:27.429: E/AndroidRuntime(31304): java.lang.NullPointerException
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.laurenswuyts.witpa.ListEventAdapter.<init>(ListEventAdapter.java:23)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.laurenswuyts.witpa.EventFragment.parseEventFromFQLResponse(EventFragment.java:154)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.laurenswuyts.witpa.EventFragment$2.onCompleted(EventFragment.java:119)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.facebook.Request$4.run(Request.java:1669)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.os.Handler.handleCallback(Handler.java:615)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.os.Looper.loop(Looper.java:137)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.app.ActivityThread.main(ActivityThread.java:4944)
11-24 13:00:27.429: E/AndroidRuntime(31304): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 13:00:27.429: E/AndroidRuntime(31304): at java.lang.reflect.Method.invoke(Method.java:511)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-24 13:00:27.429: E/AndroidRuntime(31304): at dalvik.system.NativeStart.main(Native Method)
这是我的主要活动的一部分,我开始我的片段:
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
Fragment newFragment = new EventFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
// Commit the transaction
transaction.commit();
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
Fragment newFragment = new LoginFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
// Commit the transaction
transaction.commit();
}
fragment_countainer位于我的main.xml
中<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这是我的EventFragment:
public class EventFragment extends Fragment {
//Arraylist with event details
private static List<EventDetails> dataEvent;
//Adapter to put event details in listview
ListEventAdapter eventAdapter;
//Listview with the events
ListView lvEvents;
//To show Progress Circle
LinearLayout linearProgress;
private static final String TAG = "EventFragment";
private static final int REAUTH_ACTIVITY_CODE = 100;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.event,
container, false);
linearProgress = (LinearLayout) view.findViewById(R.id.linProgress);
lvEvents = (ListView) view.findViewById(R.id.lvEvent);
// Check for an open session
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// Get the user's data
ShowEventsFromFQL();
}
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
public void ShowEventsFromFQL(){
// Check for an open session
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
//Progressbar active
linearProgress.setVisibility(View.VISIBLE);
// Get the user's data
// ORDER BY attending_count DESC
String fqlQuery = ".....";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session2 = Session.getActiveSession();
Request request = new Request(session2,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
Log.i(TAG, "Result: " + response.toString());
parseEventFromFQLResponse(response);
}
});
Request.executeBatchAsync(request);
}
}
public final void parseEventFromFQLResponse( Response response )
{
dataEvent = new ArrayList<EventDetails>();
try
{
GraphObject graphObj = response.getGraphObject();
JSONObject jsonObj = graphObj.getInnerJSONObject();
JSONArray jsonArr = jsonObj.getJSONArray( "data" );
for ( int i = 0; i < ( jsonArr.length() ); i++ )
{
JSONObject jObject = jsonArr.getJSONObject( i );
int attending_count = jObject.getInt("attending_count");
String name = jObject.getString("name");
String host = jObject.getString("host");
String description = jObject.getString("description");
String location = jObject.getString("location");
String start_time = jObject.getString("start_time");
String end_time = jObject.getString("end_time");
dataEvent.add(new EventDetails(attending_count, name, host, description, location, start_time, end_time));
}
// if(dataEvent != null){
eventAdapter = new ListEventAdapter(getActivity(), R.layout.event_item, dataEvent);
lvEvents.setAdapter(eventAdapter);
eventAdapter.notifyDataSetChanged();
//Progressbar gone
linearProgress.setVisibility(View.GONE);
// }
}
catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REAUTH_ACTIVITY_CODE) {
uiHelper.onActivityResult(requestCode, resultCode, data);
}
}
private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
if (session != null && session.isOpened()) {
// Get the user's data.
ShowEventsFromFQL();
}
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(final Session session, final SessionState state, final Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
uiHelper.onSaveInstanceState(bundle);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
}
这是我的EventFragment布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="@+id/spinnerProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
</ProgressBar>
</LinearLayout>
<ListView
android:id="@+id/lvEvent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp"/>
</RelativeLayout>
这是我的适配器:
public class ListEventAdapter extends ArrayAdapter<EventDetails> {
public ListEventAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<EventDetails> eItems;
public ListEventAdapter(Context context, int resource, List<EventDetails> eItems) {
super(context, resource, eItems);
this.eItems = eItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView txtAttending_count = null;
TextView txtName = null;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.event_item, null);
}
txtAttending_count = (TextView) v.findViewById(R.id.attending_count);
txtName = (TextView) v.findViewById(R.id.name);
EventDetails p = eItems.get(position);
if (p != null) {
if (txtAttending_count != null) {
txtAttending_count.setText(""+p.getAttending_count());
}
if (txtName != null) {
txtName.setText(""+p.getName());
}
}
return v;
}
}
任何人都可以帮助我,我真的没有找到问题。
答案 0 :(得分:0)
在创建像
这样的片段时,必须将适配器数据传递给片段LoginFragment f = LoginFragment.newInstance(strings);
public LoginFragment() {
private final String KEY = "KEY";
private ArrayList<String> mStrings;
static public LoginFragment newInstance(ArrayList<String> strings) {
Bundle b= new Bundle();
b.putStringArrayList(KEY, strings)
}
void onCreate(Bundle b) {
mStrings = getArguments().getStringArrayList(KEY);
}
}
然后在片段
中随时使用hamStrings