Reputation: 681
I have a problem with CustomListViewAddapter in Fragment. I have main activity which has slide menu and I try to show a CustomListView in it. and I get this error when I tried to use try-catch
here is part of my main activity that call fragment from slide menu
@SuppressLint("NewApi")public class MainActivity extends Activity {
.......Some Code.....
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
private void displayView(int position) {
Log.i("Position***", position + "");
// update the main content by replacing fragments
Fragment fragment = null;
Fragment fragment1 = null;
switch (position) {
case 0:
fragment = new HomeFragment();
fragment1 = fragment;
Log.i("fragment1", fragment1 + "");
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
SOme Code
}
he is my HomeFragment.java
public class HomeFragment extends ListFragment implements OnClickListener {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragProcatView = inflater.inflate(R.layout.fragment_home, container,false);
MainListView = (ListView) fragProcatView.findViewById(R.id.list);
/*
* ListActivity has a default layout that consists of a single,
* full-screen list in the center of the screen. However,we can
* customize the screen layout by setting our own view layout with
* setContentView() in onCreate(). To do this, our layout MUST contain a
* ListView object with the id "@android:id/list". ( NOT
* android:id="@+id/list" ). if we define a ListView with the android:id
* attribute set to @+id/list then it will throw below RuntimeException
*/
dataSource = new ETPUPDataSource(getActivity());
dataSource.open();
addedETPUP = dataSource.getAllRecordList();
dataSource.close();
Log.i("DBCount", addedETPUP.size() + "");
// if this is first time that we run App so maybe we dont have any data
// on it so we need to check that
// And call our list View again
if (addedETPUP.size() == 0) {
if (InternetStatus.getInstance(getActivity()).haveNetworkConnection(getActivity())) {
Log.i("XXXX return haveConnectedWifi", "return haveConnectedWifiXXXX");
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setMessage("There Is No Dara To Show. Do You Want To Add New Data?");
alertBuilder.setCancelable(false);
alertBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Call Json To Get Data From Server
new JSONParse().execute();
refreshList();
}
});
alertBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
Toast.makeText(
getActivity(),
"There Is No Internet Connection Available, Please Make Sure Your Device Connected To InterNet Via WIFI Or Mobile Internet!",
Toast.LENGTH_LONG).show();
}
}
// Create New object from your CustomListViewAddapter and pass this
// context and your list of ETPUP as data
try{
ArrayAdapter<getAllData> adapterMain = new CustomListViewAddapter(getActivity(), addedETPUP);
MainListView.setAdapter(adapterMain);
//MainListView.setAdapter(new CustomListViewAddapter(getActivity(), addedETPUP));
}catch(Exception e){
Log.e("Error Happened ", "During creating List view");
Log.e("Error Happened ", e.toString());
}
// registerForContextMenu(MainListView.getListView());
BtnGetData = (Button) getView().findViewById(R.id.getdata);
BtnGetData.setOnClickListener(this);
btnShowFav = (Button) getView().findViewById(R.id.ShowFav);
btnShowFav.setOnClickListener(this);
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.getdata:
new JSONParse().execute();
case R.id.ShowFav:
Log.i("show Fav", "in show Fav btn");
Intent ShowFav = new Intent(getActivity(), ShowFavorite.class);
startActivity(ShowFav);
break;
}
}
public void refreshList() {
Log.i("Refresh List", "I'm in Refresh List");
// Create New object from your CustomListViewAddapter and pass this
// context and your list of ETPUP as data
dataSource.open();
addedETPUP = dataSource.getAllRecordList();
dataSource.close();
Log.i("DBCount*****00******", addedETPUP.size() + "");
try{
ArrayAdapter<getAllData> adapterMain = new CustomListViewAddapter(getActivity(), addedETPUP);
MainListView.setAdapter(adapterMain);
}catch(Exception e){
Log.e("Error Happened In Refresh", "During creating List view");
Log.e("Error Happened ", e.toString());
}
// registerForContextMenu(((ListActivity) MainListView).getListView());
// This Just Show Simple List
/*
* ArrayAdapter<getAllData> adapter = new ArrayAdapter<getAllData>(this,
* android.R.layout.simple_expandable_list_item_1,addedZekrFields2);
* setListAdapter(adapter); registerForContextMenu(getListView());
*/
}
and here is my Log Cat
06-01 23:24:38.861: D/dalvikvm(1873): GC_FOR_ALLOC freed 96K, 6% free 3142K/3308K, paused 30ms, total 32ms
Blockquote
06-01 23:24:38.871: I/dalvikvm-heap(1873): Grow heap (frag case) to 4.207MB for 1127536-byte allocation
06-01 23:24:38.911: D/dalvikvm(1873): GC_FOR_ALLOC freed 2K, 4% free 4240K/4412K, paused 31ms, total 31ms
06-01 23:24:39.021: I/Position***(1873): 2
06-01 23:24:39.331: D/gralloc_goldfish(1873): Emulator without GPU emulation detected.
06-01 23:24:42.801: I/in Draw Click***(1873): 000000000000011123
06-01 23:24:42.801: I/Position***(1873): 0
06-01 23:24:42.811: I/fragment1(1873): HomeFragment{b1f98308}
06-01 23:24:43.061: D/dalvikvm(1873): GC_FOR_ALLOC freed 80K, 4% free 4657K/4816K, paused 24ms, total 29ms
06-01 23:24:43.121: I/ETPUP(1873): Database open
06-01 23:24:43.141: I/ETPUP(1873): Database close
06-01 23:24:43.141: I/DBCount(1873): 3
06-01 23:24:43.161: E/Error Happened(1873): During creating List view
06-01 23:24:43.161: E/Error Happened(1873): java.lang.NullPointerException
06-01 23:24:43.161: D/AndroidRuntime(1873): Shutting down VM
06-01 23:24:43.161: W/dalvikvm(1873): threadid=1: thread exiting with uncaught exception
(group=0xb1b08ba8)
06-01 23:24:43.181: E/AndroidRuntime(1873): FATAL EXCEPTION: main
06-01 23:24:43.181: E/AndroidRuntime(1873): Process: com.PishgamanIt.ETPUP, PID: 1873
06-01 23:24:43.181: E/AndroidRuntime(1873): java.lang.NullPointerException
06-01 23:24:43.181: E/AndroidRuntime(1873): at com.PishgamanIt.ETPUP.HomeFragment.onCreateView(HomeFragment.java:171)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.app.Fragment.performCreateView(Fragment.java:1700)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.app.BackStackRecord.run(BackStackRecord.java:684)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.os.Handler.handleCallback(Handler.java:733)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.os.Handler.dispatchMessage(Handler.java:95)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.os.Looper.loop(Looper.java:136)
06-01 23:24:43.181: E/AndroidRuntime(1873): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-01 23:24:43.181: E/AndroidRuntime(1873): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 23:24:43.181: E/AndroidRuntime(1873): at java.lang.reflect.Method.invoke(Method.java:515)
06-01 23:24:43.181: E/AndroidRuntime(1873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-01 23:24:43.181: E/AndroidRuntime(1873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-01 23:24:43.181: E/AndroidRuntime(1873): at dalvik.system.NativeStart.main(Native Method)
06-01 23:29:43.301: I/Process(1873): Sending signal. PID: 1873 SIG: 9
here is my main xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
/>
and here is my fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout1111111111111"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/getdata"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/getdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ShowFav"
android:layout_alignBottom="@+id/ShowFav"
android:layout_alignLeft="@+id/list"
android:text="Get Data" />
<Button
android:id="@+id/ShowFav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@android:id/list"
android:layout_marginBottom="14dp"
android:layout_marginRight="16dp"
android:text="Show Favorites" />
</RelativeLayout>
I did a lot of researches and I'm stuck here for almost a week :(
I really appricate any help and advice
Upvotes: 0
Views: 101
Reputation: 47807
First correct this you should
return fragProcatView;
instead of
return inflater.inflate(R.layout.fragment_home, container, false);
in onCreateView(.....)
in your HomeFragment
First remove ListFragment
and extends a simple Fragment
to your HomeFragment
your should change this android:id="@android:id/list"
to android:id="@+id/list"
and then you should change this
BtnGetData = (Button) getView().findViewById(R.id.getdata)
btnShowFav = (Button) getView().findViewById(R.id.ShowFav);
to
BtnGetData = (Button) fragProcatView.findViewById(R.id.getdata)
btnShowFav = (Button) fragProcatView.findViewById(R.id.ShowFav);
Upvotes: 2