Using simple fragments in android - java

I am trying to use Simple Fragments
I am getting the log errors in the screen
Any ideas to resolve them
LM_Fragement.java
public class LM_Fragement extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
return inflater.inflate(
R.layout.lm_fragement, container, false);
}
}
PM_Fragement.java
public class PM_Fragement extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
return inflater.inflate(
R.layout.pm_fragment, container, false);
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
/**
* Check the device orientation and act accordingly
*/
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
/**
* Landscape mode of the device
*/
LM_Fragement ls_fragment = new LM_Fragement();
fragmentTransaction.replace(android.R.id.content, ls_fragment);
}else{
/**
* Portrait mode of the device
*/
PM_Fragement pm_fragment = new PM_Fragement();
fragmentTransaction.replace(android.R.id.content, pm_fragment);
}
fragmentTransaction.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="com.example.fragments"
android:id="#+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragments"
android:id="#+id/pm_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Log::
10-13 19:36:11.917: I/dalvikvm(652): Could not find method com.example.fragmentstutorialpoint.MainActivity.getFragmentManager, referenced from method com.example.fragmentstutorialpoint.MainActivity.onCreate
10-13 19:36:11.966: E/AndroidRuntime(652): FATAL EXCEPTION: main
10-13 19:36:11.966: E/AndroidRuntime(652): java.lang.NoSuchMethodError: com.example.fragmentstutorialpoint.MainActivity.getFragmentManager
10-13 19:36:11.966: E/AndroidRuntime(652): at com.example.fragmentstutorialpoint.MainActivity.onCreate(MainActivity.java:22)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.os.Looper.loop(Looper.java:123)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 19:36:11.966: E/AndroidRuntime(652): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 19:36:11.966: E/AndroidRuntime(652): at java.lang.reflect.Method.invoke(Method.java:507)
10-13 19:36:11.966: E/AndroidRuntime(652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 19:36:11.966: E/AndroidRuntime(652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 19:36:11.966: E/AndroidRuntime(652): at dalvik.system.NativeStart.main(Native Method)
How can i resolve these errors ....
Any Ideas
Thanks

Are you using the supportlibrary in a pre 3.0 project?
If so use getSupportFragmentManager instead

Related

Why DrawerLayout and ActionBar not working on Tablets?

I'm develop some app with sliding menu.
when i try to run this on my galaxy 4s(api 18+ 4.3.3) its work fine!
but when i run this on xperia arc (api 8 ver 2.3.4) the app crash.
i know its dependent on Support library and i use :
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
and i need to use :
import android.support.v7.app.ActionBar;
so i added to my project the support library and connect the support library to him.
but i dont know how to handle the support and what i need to chage on my code:
my java code (v4){
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
public class MainActivity extends Activity implements OnClickListener, OnLongClickListener , OnTouchListener , OnItemClickListener{
/////////////////
ArrayList<ItemInSetting > listSetting = new ArrayList<ItemInSetting>();
CustomAdapterSetting adapterSetting;
DrawerLayout drawer;
ActionBarDrawerToggle toggle;
//////////////////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingItems();
}
public void settingItems(){
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawer = (DrawerLayout) findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this, drawer, R.drawable.ic_drawer, 0, 0){
#Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("menu");
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle("app title");
invalidateOptionsMenu();
}
};drawer.setDrawerListener(toggle);
listSetting.add(new ItemInSetting("ist1", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist2", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist3", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist4", R.drawable.calende_bigg));
lvSetting = (ListView) findViewById(R.id.listSetting);
lvSetting.setOnItemClickListener(this);
adapterSetting = new CustomAdapterSetting(listSetting, this);
lvSetting.setAdapter(adapterSetting);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
date = new Date();
switch (position) {
case 0:
Intent intent = new Intent (MainActivity.this,ListOfHours.class);
startActivity(intent);
break;
case 1:
getSetting();
if(sickIsOn){
cancelingFreeday(false);
}else{
addingManualMenu();
}
break;
case 2:
changingSetting(" ");
break;
case 3:
sendContact();
break;
default:
break;
}
drawer.closeDrawers();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (toggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
toggle.onConfigurationChanged(newConfig);
}
my xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="#drawable/new_back"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listSetting"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#android:color/darker_gray"
android:choiceMode="singleChoice"
android:listSelector="#drawable/list_selector" >
</ListView>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bib.workclock"
android:versionCode="52"
android:versionName="3.0.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/icon_new3"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity
android:name="com.bibas.workclock.MainActivity"
android:label="#string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ListOfHours" ></activity>
<activity android:name="HelpPage"></activity>
<activity android:name="info" android:screenOrientation="portrait"></activity>
</application>
</manifest>
this the locat error when i try to run on xperia :
05-04 01:59:05.058: E/AndroidRuntime(15484): FATAL EXCEPTION: main
05-04 01:59:05.058: E/AndroidRuntime(15484): java.lang.NoSuchMethodError: com.bba.workclock.MainActivity.getActionBar
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.bba.workclock.MainActivity.settingItems(MainActivity.java:1657)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.bba.workclock.MainActivity.onCreate(MainActivity.java:124)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.os.Looper.loop(Looper.java:130)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.main(ActivityThread.java:3701)
05-04 01:59:05.058: E/AndroidRuntime(15484): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 01:59:05.058: E/AndroidRuntime(15484): at java.lang.reflect.Method.invoke(Method.java:507)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
05-04 01:59:05.058: E/AndroidRuntime(15484): at dalvik.system.NativeStart.main(Native Method)
here is project like mine:
http://speedy.sh/UKsrV/SlidingMenu.zip
if any one can fix that code and give it to me back i appreciate alot
How can i fix this?!!
please help me :(((
I FIX THAT PROBLEM BUT KNOW ON MY TABLET IT'S CRASH .
here the locat error:
05-11 23:10:22.127: E/AndroidRuntime(31723): FATAL EXCEPTION: main
05-11 23:10:22.127: E/AndroidRuntime(31723): java.lang.IllegalStateException: Child android.widget.FrameLayout#41f91e40 at index 0 does not have a valid layout_gravity - must be Gravity.LEFT, Gravity.RIGHT or Gravity.NO_GRAVITY
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:721)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer.doFrame(Choreographer.java:525)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Handler.handleCallback(Handler.java:615)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Looper.loop(Looper.java:137)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.app.ActivityThread.main(ActivityThread.java:4895)
05-11 23:10:22.127: E/AndroidRuntime(31723): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 23:10:22.127: E/AndroidRuntime(31723): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-11 23:10:22.127: E/AndroidRuntime(31723): at dalvik.system.NativeStart.main(Native Method)
ActionBar is available from api level 11. To support below api level 11 you need to use AppCompat from the support library. Reference AppCompat in your android project.
You need to extend ActionBarActivity. Use theme derived from Theme.AppCompat in manifest and use getSupportActionBar().
Complete example #
Navigation Drawer with backword compatibility android
Check Adding libraries with resources
https://developer.android.com/tools/support-library/setup.html
And read
http://developer.android.com/guide/topics/ui/actionbar.html

java.lang.IllegalArgumentException: No view found for id 0x7f0600cc for fragment

I have implemented the ViewPager in Fragment in my application which contains the Custom layout. Now in Custom layout of ViewPager there is a TextView and onClick of the TextView i would like to replace the current fragment with other fragment. Lets say with Fragment2.
I already tried to replace but its giving error for the Resource id of the FrameLayout where i want to load the fragment. I already checked the relevant solutions in SO but none helped me.
Please any one can guide me for this error. It would be really grateful and appreciable.
Logcat:
11-13 11:17:54.480: E/AndroidRuntime(3982): FATAL EXCEPTION: main
11-13 11:17:54.480: E/AndroidRuntime(3982): java.lang.IllegalArgumentException: No view found for id 0x7f0600cc for fragment OtherProfileFragment{40ceba28 #4 id=0x7f0600cc}
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:865)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.os.Handler.handleCallback(Handler.java:725)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.os.Looper.loop(Looper.java:137)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-13 11:17:54.480: E/AndroidRuntime(3982): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 11:17:54.480: E/AndroidRuntime(3982): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 11:17:54.480: E/AndroidRuntime(3982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-13 11:17:54.480: E/AndroidRuntime(3982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-13 11:17:54.480: E/AndroidRuntime(3982): at dalvik.system.NativeStart.main(Native Method)
Here is my code where onclick of TextView i am trying to load the Fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_feed_detail, container,
false);
m_tvProfileName = (TextView) v.findViewById(R.id.afd_tvProfileName);
m_tvProfileName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager m_mange = m_context.getSupportFragmentManager();
FragmentTransaction fm = m_mange.beginTransaction();
Fragment llf = new OtherFragment();
Bundle m_bundle = new Bundle();
m_bundle.putInt("Id", Id);
llf.setArguments(m_bundle);
fm.addToBackStack("Other");
fm.replace(R.id.fragmentswitcherframe, llf); //This line throwing error
fm.commit();
getActivity().finish();
}
});
return v;
}

PullToRefreshGridView Inflate Exception

I am using this library https://github.com/chrisbanes/Android-PullToRefresh for implementing a refreshable grid view and i am getting this error:
android.view.InflateException: Binary XML file line #8: Error inflating class com.handmark.pulltorefresh.library.PullToRefreshGridView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at com.example.retrievetweets.FragmentPhotos.onCreateView(FragmentPhotos.java:55)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:238)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 24 more
Caused by: java.lang.NullPointerException
at com.handmark.pulltorefresh.library.internal.IndicatorLayout.<init>(IndicatorLayout.java:66)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.addIndicatorViews(PullToRefreshAdapterViewBase.java:355)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.updateUIForMode(PullToRefreshAdapterViewBase.java:328)
at com.handmark.pulltorefresh.library.PullToRefreshBase.init(PullToRefreshBase.java:1142)
at com.handmark.pulltorefresh.library.PullToRefreshBase.<init>(PullToRefreshBase.java:113)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.<init>(PullToRefreshAdapterViewBase.java:74)
at com.handmark.pulltorefresh.library.PullToRefreshGridView.<init>(PullToRefreshGridView.java:35)
... 27 more
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- The PullToRefreshGridView replaces a standard GridView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="1dp"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:gravity="fill"
ptr:ptrMode="both"
ptr:ptrDrawable="#drawable/default_ptr_rotate"
/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:indeterminateOnly="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
And here is the java code for the method onCreateView in FragmentPhotos (the line 55 is this one "ll = (RelativeLayout) inflater.inflate(R.layout.activity_grid_view,"):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ref = "";
fa = getActivity();
ll = (RelativeLayout) inflater.inflate(R.layout.activity_grid_view,
container, false);
bar = (ProgressBar) ll.findViewById(R.id.progressBar);
mPullRefreshGridView = (PullToRefreshGridView) ll
.findViewById(R.id.pull_refresh_grid);
mGridView = mPullRefreshGridView.getRefreshableView();
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshGridView
.setOnRefreshListener(new OnRefreshListener<GridView>() {
#Override
public void onRefresh(
PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
if (NetworkReceiver.mobileConnected) {
new GetDataTaskWhitoutLoading().execute();
} else {
Toast.makeText(
getActivity(),
"Ahora mismo no se pueden cargar nuevos datos."
+ " Comprueba la conexi—n a Internet.",
Toast.LENGTH_LONG).show();
mPullRefreshGridView.onRefreshComplete();
}
}
});
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// Do something in response to the click
Intent intent = new Intent(fa, ImageSelected.class);
intent.putExtra("URL_image", mListItems.get(position)
.subSequence(0, mListItems.get(position).length() - 6));
startActivity(intent);
}
});
mListItems = new ArrayList<String>();
mAdapter = new FotosItemAdapter(fa, R.layout.image_group, mListItems);
mGridView.setAdapter(mAdapter);
if (NetworkReceiver.mobileConnected) {
GetDataTask.newInstance(bar).execute();
}
return ll;
}
Thank you!!
Are you importing in your classhpath on eclipse?
Pull to refhresh is a lib for android,then import like this:
project properties> android> add lib.
In my case it solved by adding this to my project.properties :
target=android-19
android.library.reference.1=../library
that library is the name of the handmark library.
hope be useful.

Unfortunately app has stopped working

I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display is supposed to reference a TextView instance, but you're explictly casting into a Button, and these are not compatible types.
As you got the answer from A-C this time, but for next time in android application development a important suggestion:
Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error.
And try to find what can be wrong with that line of code.
For example: in your logcat it is showing-
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button. So you can easily remove your errors without any help.
Hope that helps not only you current problem but prevented your future time and efforts.

NullPointException in ListView

Please have a look at the following code
activity_form.xml
<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"
tools:context=".Form" >
<ImageView
android:id="#+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_marginTop="10dp"
android:text="#string/title" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/listImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star" />
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Form.java
package com.example.callmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Form extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
ListView lv = (ListView)findViewById(android.R.id.list);
String arr[] = getResources().getStringArray(R.array.branch_list);
lv.setAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.listText,arr));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_form, menu);
return true;
}
private class MyAdapter extends ArrayAdapter
{
public MyAdapter(Context context, int resource, int textViewResourceId,
Object[] objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View contentView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_layout, parent,false);
TextView tv = (TextView)findViewById(R.id.listText);
ImageView iv = (ImageView)findViewById(R.id.listImage);
String arr[] = getResources().getStringArray(R.array.branch_list);
tv.setText(arr[position]);
if(arr[position].equals("Anuradhapura"))
iv.setImageResource(R.drawable.star);
return view;
}
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Esoft Call Manager</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title"><b>Select a Branch To Call</b></string>
<string-array name="branch_list">
<item>Anuradhapura</item>
<item>Avissawella</item>
</string-array>
</resources>
When I run this code, I get the 'NullPointException'. I am adding the complete log file here
02-08 21:04:01.463: D/dalvikvm(490): GC_EXTERNAL_ALLOC freed 43K, 53% free 2551K/5379K, external 1949K/2137K, paused 82ms
02-08 21:04:01.713: D/AndroidRuntime(490): Shutting down VM
02-08 21:04:01.713: W/dalvikvm(490): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-08 21:04:01.733: E/AndroidRuntime(490): FATAL EXCEPTION: main
02-08 21:04:01.733: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callmanager/com.example.callmanager.Form}: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Looper.loop(Looper.java:123)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 21:04:01.733: E/AndroidRuntime(490): at dalvik.system.NativeStart.main(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): Caused by: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at com.example.esoftcallmanager.Form.onCreate(Form.java:24)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-08 21:04:01.733: E/AndroidRuntime(490): ... 11 more
I am adding the folder structure below if it helps
Why I am getting this error? How to solve this? Please help!
try this:
change this
ListView lv = (ListView)findViewById(android.R.id.list);
for:
ListView lv = (ListView)findViewById(R.id.list);
Normally they use android.R.id.list when class extends a ListActivity
Change:
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Unless you have an id in your XML in the format of android:id="#android:id/myID", you don't need to use android.R. Whenever you use one of your own IDs, you can simply use R.id.Whatever
The issue is right here. ListView lv = (ListView)findViewById(android.R.id.list); you reference the Id android.R.id.list
however in your layout you reference android:id="#+id/list" which creates an Id in the Resources class called list. so you should be referencing R.id.list if you debug you will find that the lv variable is null because it wasn't found because you referenced the wrong id.
If your are using your own layout you don't need any more to write android.R just remove android and reorganize your class
Try this.
I hope it will solve your NPE.
Change it
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Actually You have defined the list view in the layout activity_form.xml have name list so you need to write the R.id.list.
I managed to solve this by my self. Following edits were required.
activity_form.xml
The "id" of the ListView should be
android:id="#android:id/list"
Form.java
It should call the ListView in this way
ListView lv = (ListView) findViewById(android.R.id.list);
Inside getView(), the initialization of the TextView and ImageView should be like this
TextView tv = (TextView) view.findViewById(R.id.listText);
ImageView iv = (ImageView) view.findViewById(R.id.listImage);
Note the view.findViewById() part.

Categories