我有一个带有customAdapter的Fragment Activity,但是setAdapter给我一个nullpointerexception。 在CustomAdapter中,我返回一个ParseQuery。 我无法将Fragment扩展到ListFragment,因为标签停止工作。 请帮我找到错误。
MainActivity.java
package com.comunadigital.cuidatemasmaterial;
import android.app.ListFragment;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.comunadigital.cuidatemasmaterial.fragments.FragmentDirectorio;
import com.comunadigital.cuidatemasmaterial.fragments.FragmentFichas;
import com.comunadigital.cuidatemasmaterial.fragments.FragmentForo;
import com.comunadigital.cuidatemasmaterial.fragments.FragmentMedicinas;
import com.parse.GetCallback;
import com.parse.ParseACL;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;
public class MainActivity extends ActionBarActivity implements MaterialTabListener {
public static final String TAG = MainActivity.class.getSimpleName();
protected ProgressBar mProgressBar;
//int representing our 0th tab corresponding to the Fragment where search results are dispalyed
public static final int TAB_FICHAS = 0;
//int corresponding to our 1st tab corresponding to the Fragment where box office hits are dispalyed
public static final int TAB_DIRECTORIO = 1;
//int corresponding to our 2nd tab corresponding to the Fragment where upcoming movies are displayed
public static final int TAB_MEDICINAS = 2;
// int corresponde al cuarto tab
public static final int TAB_FORO = 3;
//int corresponding to the number of tabs in our Activity
public static final int TAB_COUNT = 4;
private Toolbar mToolbar;
private ViewGroup mContainerToolbar;
private MaterialTabHost mTabHost;
private ViewPager mPager;
private ViewPagerAdapter mAdapter;
//private FragmentDrawer mDrawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupTabs();
setupDrawer();
}
private void setupDrawer() {
mToolbar = (Toolbar) findViewById(R.id.app_bar);
mContainerToolbar = (ViewGroup) findViewById(R.id.container_app_bar);
//set the Toolbar as ActionBar
setSupportActionBar(mToolbar);
mToolbar.setNavigationIcon(R.drawable.iconbar);
try{
getSupportActionBar().setDisplayShowHomeEnabled(true);
}catch(NullPointerException e){
e.printStackTrace();
}
}
private void setupTabs() {
mTabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
mPager = (ViewPager) findViewById(R.id.viewPager);
mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mAdapter);
//when the page changes in the ViewPager, update the Tabs accordingly
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mTabHost.setSelectedNavigationItem(position);
}
});
//Add all the Tabs to the TabHost
for (int i = 0; i < mAdapter.getCount(); i++) {
mTabHost.addTab(
mTabHost.newTab()
.setIcon(mAdapter.getIcon(i))
.setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.logoutButton) {
/*
* Log current user out using ParseUser.logOut()
*/
ParseUser.logOut();
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(MaterialTab materialTab) {
//when a Tab is selected, update the ViewPager to reflect the changes
mPager.setCurrentItem(materialTab.getPosition());
}
@Override
public void onTabReselected(MaterialTab materialTab) {
}
@Override
public void onTabUnselected(MaterialTab materialTab) {
}
//FragmentStatePagerAdapter
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
//AQUI PONEMOS LOS ICONOS GRAFICOS
int icons[] = {R.drawable.ficha,
R.drawable.doctor,
R.drawable.medicina,
R.drawable.foro};
FragmentManager fragmentManager;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
fragmentManager = fm;
}
@Override
public Fragment getItem(int num) {
Fragment fragment = null;
// L.m("getItem called for " + num);
switch (num) {
case TAB_FICHAS:
fragment = FragmentFichas.newInstance(getApplicationContext());
break;
case TAB_DIRECTORIO:
fragment = FragmentDirectorio.newInstance();
break;
case TAB_MEDICINAS:
fragment = FragmentMedicinas.newInstance();
break;
case TAB_FORO:
fragment = FragmentForo.newInstance();
break;
}
return fragment;
}
@Override
public int getCount() {
return TAB_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return getResources().getStringArray(R.array.tabs)[position];
}
private Drawable getIcon(int position) {
return getResources().getDrawable(icons[position]);
}
}
}
FragmentFichas.java
package com.comunadigital.cuidatemasmaterial.fragments;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.comunadigital.cuidatemasmaterial.DoctoresActivity;
import com.comunadigital.cuidatemasmaterial.MainActivity;
import com.comunadigital.cuidatemasmaterial.R;
import com.comunadigital.cuidatemasmaterial.adapters.CustomAdapter;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseUser;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Use the {@link FragmentFichas#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentFichas extends Fragment {
private static Context mContext;
private ParseQueryAdapter<ParseObject> mainAdapter;
public FragmentFichas() {
// Required empty public constructor
}
public static FragmentFichas newInstance(Context context) {
mContext = context;
return new FragmentFichas();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView mList = (ListView) getActivity().findViewById(R.id.list);
CustomAdapter urgentTodosAdapter = new CustomAdapter(getActivity());
mList.setAdapter(urgentTodosAdapter);
urgentTodosAdapter.loadObjects();
}
@Override
public void onResume() {
super.onResume();
//urgentTodosAdapter.loadObjects();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fichas, container, false);
/*
mList = (ListView) getActivity().findViewById(R.id.list);
urgentTodosAdapter = new CustomAdapter(container.getContext());
mList.setAdapter(urgentTodosAdapter);
urgentTodosAdapter.loadObjects();
*/
return view;
}
}
CustomAdapter.java
package com.comunadigital.cuidatemasmaterial.adapters;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.parse.ParseFile;
import com.parse.ParseImageView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseUser;
import com.comunadigital.cuidatemasmaterial.R;
/**
* Created by proyectos1 on 15/07/2015.
*/
public class CustomAdapter extends ParseQueryAdapter<ParseObject> {
public CustomAdapter(Context context) {
// Use the QueryFactory to construct a PQA that will only show
// Todos marked as high-pri
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
ParseQuery query = new ParseQuery("FichasMedicas");
query.whereEqualTo("user", ParseUser.getCurrentUser());
return query;
}
});
}
// Customize the layout by overriding getItemView
@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.urgent_item, null);
}
super.getItemView(object, v, parent);
// Add and download the image
ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile imageFile = object.getParseFile("foto");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
// Add the title view
TextView titleTextView = (TextView) v.findViewById(R.id.text1);
titleTextView.setText(object.getString("nombre"));
// Add a reminder of how long this item has been outstanding
//TextView timestampView = (TextView) v.findViewById(R.id.timestamp);
//timestampView.setText(object.getCreatedAt().toString());
return v;
}
}
ActivityMain.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<it.neokree.materialtabs.MaterialTabHost
android:id="@+id/materialTabHost"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@+id/app_bar"
app:accentColor="@color/colorAccent"
app:hasIcons="true"
app:iconColor="@android:color/white"
app:primaryColor="@color/colorPrimary" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/materialTabHost"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
FragmentFichas.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
LOGERROR
07-20 15:46:45.375 9596-9596/com.comunadigital.cuidatemasmaterial E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.comunadigital.cuidatemasmaterial, PID: 9596
java.lang.NullPointerException
at com.comunadigital.cuidatemasmaterial.fragments.FragmentFichas.onCreate(FragmentFichas.java:59)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:846)
at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:860)
at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:152)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1103)
at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
at android.view.View.measure(View.java:16497)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您需要从片段中的膨胀视图中获取ListView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fichas, container, false);
//It's not in the Activity layout:
//mList = (ListView) getActivity().findViewById(R.id.list);
//It's in the Fragment layout:
mList = (ListView) view.findViewById(R.id.list);
urgentTodosAdapter = new CustomAdapter(getActivity());
mList.setAdapter(urgentTodosAdapter);
urgentTodosAdapter.loadObjects();
return view;
}
还要确保将urgentTodosAdapter
声明为实例变量:
public class FragmentFichas extends Fragment {
private static Context mContext;
private ParseQueryAdapter<ParseObject> mainAdapter;
private CustomAdapter urgentTodosAdapter; //added
//.............