How to get Options Menu to work on Nexus 7? - java

I am programming an app with an Options Menu in my Main Activity. This Options Menu works fine on my HTC Android Incredible device. However, when I test it with a Nexus 7 emulator the program crashes. How can I get the options menu to work on the Nexus 7? Any ideas on this? Thank you in advance. Here is the code I am using.
package com.carouseldemo.main;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselAdapter.AdapterContextMenuInfo;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.controls.CarouselItem;
public class MainActivity extends Activity {
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.testmenu1, menu);
// your code...
return true;} // true if you want the menu to be displayed; false otherwise
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// Just prepare ourself for unexpected arguments...
if (menuInfo == null) {
return;
}
if (!(menuInfo instanceof AdapterContextMenuInfo)) {
return;
}
AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo;
switch (mi.position) {
case 0:
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
break;
case 1:
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
break;
case 2:
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
break;
case 3:
menu.add("mouse menu");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
break;
case 4:
menu.add("panda menu");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
break;
case 5:
menu.add("rabbit menu");
menu.add("Product 1");
menu.add("Product 1");
menu.add("Product 1");
break;
default:
break;
}
super.onCreateContextMenu(menu, v, menuInfo);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Carousel carousel = (Carousel) findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
CarouselItem item = (CarouselItem) parent.getChildAt(position);
// HERE: Force context menu
carousel.showContextMenuForChild(item);
Toast.makeText(
MainActivity.this,
String.format("%s has been clicked",
((CarouselItem) parent.getChildAt(position))
.getName()), Toast.LENGTH_SHORT).show();
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return false;
}});
carousel.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView) (findViewById(R.id.selected_item));
switch (position) {
case 0:
txt.setText("The cat (Felis catus), also known as the domestic cat or housecat to distinguish it from other felids and felines, is a small, usually furry, domesticated, carnivorous mammal that is valued by humans for its companionship and for its ability to hunt vermin and household pests. Cats have been associated with humans for at least 9,500 years, and are currently the most popular pet in the world. Owing to their close association with humans, cats are now found almost everywhere in the world.");
break;
case 1:
txt.setText("The hippopotamus (Hippopotamus amphibius), or hippo, from the ancient Greek for \"river horse\" (), is a large, mostly herbivorous mammal in sub-Saharan Africa, and one of only two extant species in the family Hippopotamidae (the other is the Pygmy Hippopotamus.) After the elephant, the hippopotamus is the third largest land mammal and the heaviest extant artiodactyl.");
break;
case 2:
txt.setText("A monkey is a primate, either an Old World monkey or a New World monkey. There are about 260 known living species of monkey. Many are arboreal, although there are species that live primarily on the ground, such as baboons. Monkeys are generally considered to be intelligent. Unlike apes, monkeys usually have tails. Tailless monkeys may be called \"apes\", incorrectly according to modern usage; thus the tailless Barbary macaque is called the \"Barbary ape\".");
break;
case 3:
txt.setText("A mouse (plural: mice) is a small mammal belonging to the order of rodents. The best known mouse species is the common house mouse (Mus musculus). It is also a popular pet. In some places, certain kinds of field mice are also common. This rodent is eaten by large birds such as hawks and eagles. They are known to invade homes for food and occasionally shelter.");
break;
case 4:
txt.setText("The giant panda, or panda (Ailuropoda melanoleuca, literally meaning \"black and white cat-foot\") is a bear native to central-western and south western China.[4] It is easily recognized by its large, distinctive black patches around the eyes, over the ears, and across its round body. Though it belongs to the order Carnivora, the panda's diet is 99% bamboo.");
break;
case 5:
txt.setText("Rabbits (or, colloquially, bunnies) are small mammals in the family Leporidae of the order Lagomorpha, found in several parts of the world. There are eight different genera in the family classified as rabbits, including the European rabbit (Oryctolagus cuniculus), cottontail rabbits (genus Sylvilagus; 13 species), and the Amami rabbit (Pentalagus furnessi, an endangered species on Amami, Japan)");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
});
registerForContextMenu(carousel);
}
}
LogCat Error:
01-31 19:55:42.221: E/AndroidRuntime(1758): FATAL EXCEPTION: main
01-31 19:55:42.221: E/AndroidRuntime(1758): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carouseldemo.main/com.carouseldemo.main.MainActivity}: android.view.InflateException: Binary XML file line #34: Error inflating class com.carouseldemo.controls.Carousel
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.os.Looper.loop(Looper.java:137)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-31 19:55:42.221: E/AndroidRuntime(1758): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 19:55:42.221: E/AndroidRuntime(1758): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 19:55:42.221: E/AndroidRuntime(1758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 19:55:42.221: E/AndroidRuntime(1758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 19:55:42.221: E/AndroidRuntime(1758): at dalvik.system.NativeStart.main(Native Method)
01-31 19:55:42.221: E/AndroidRuntime(1758): Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class com.carouseldemo.controls.Carousel
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-31 19:55:42.221: E/AndroidRuntime(1758): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.Activity.setContentView(Activity.java:1881)
01-31 19:55:42.221: E/AndroidRuntime(1758): at com.carouseldemo.main.MainActivity.onCreate(MainActivity.java:108)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.Activity.performCreate(Activity.java:5104)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-31 19:55:42.221: E/AndroidRuntime(1758): ... 11 more
01-31 19:55:42.221: E/AndroidRuntime(1758): Caused by: java.lang.reflect.InvocationTargetException
01-31 19:55:42.221: E/AndroidRuntime(1758): at java.lang.reflect.Constructor.constructNative(Native Method)
01-31 19:55:42.221: E/AndroidRuntime(1758): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-31 19:55:42.221: E/AndroidRuntime(1758): ... 22 more
01-31 19:55:42.221: E/AndroidRuntime(1758): Caused by: java.lang.OutOfMemoryError
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.content.res.Resources.loadDrawable(Resources.java:1965)
01-31 19:55:42.221: E/AndroidRuntime(1758): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-31 19:55:42.221: E/AndroidRuntime(1758): at com.carouseldemo.controls.Carousel$ImageAdapter.SetImages(Carousel.java:778)
01-31 19:55:42.221: E/AndroidRuntime(1758): at com.carouseldemo.controls.Carousel.<init> (Carousel.java:253)
01-31 19:55:42.221: E/AndroidRuntime(1758): at com.carouseldemo.controls.Carousel.<init> (Carousel.java:204)
01-31 19:55:42.221: E/AndroidRuntime(1758): ... 25 more

Related

Checkbox in the popupMenu

I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;

coding android Accelerometer in class

I'm writing an app that show accelerometer readings, I was able yesterday to write all the accelerometer code in the main activity and it works fine on emulator and my device.
But today I was trying to make a function that contains all the accelerometer code and return the three integers X,Y,Z in integer array, so I make a code that I think it`s right but every time I run the project the emulator gives an error "Unfortunately app-name has stopped" .
So I wish any help , please .
LogCat:
04-27 20:39:27.098: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
04-27 20:39:27.118: E/AndroidRuntime(812): FATAL EXCEPTION: main
04-27 20:39:27.118: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accelerometer_sensor/com.example.accelerometer_sensor.MainActivity}: java.lang.NullPointerException
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:137)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-27 20:39:27.118: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 20:39:27.118: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 20:39:27.118: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
04-27 20:39:27.118: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
04-27 20:39:27.118: E/AndroidRuntime(812): at com.example.accelerometer_sensor.AccelerometerClass.AccelerometerInit(AccelerometerClass.java:20)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.example.accelerometer_sensor.MainActivity.onCreate(MainActivity.java:19)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.Activity.performCreate(Activity.java:5104)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-27 20:39:27.118: E/AndroidRuntime(812): ... 11 more
04-27 20:39:30.119: I/Process(812): Sending signal. PID: 812 SIG: 9
here`s my main activity code:
package com.example.accelerometer_sensor;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
int [] AccVal = new int[3];
AccelerometerClass acc = new AccelerometerClass();
TextView acceleration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acc = new AccelerometerClass();
AccVal = acc.AccelerometerInit();
acceleration = (TextView)findViewById(R.id.acceleration);
acceleration.setText("X: "+AccVal[0]+
"Y: "+AccVal[1]+
"Z: "+AccVal[2]);
}
#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_main, menu);
return true;
}
}
and this is my AccelerometerClass Code ::
package com.example.accelerometer_sensor;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class AccelerometerClass implements SensorEventListener
{
private static Context context;
int [] Vals = new int[3];
public int [] AccelerometerInit()
{
Sensor accelerometer;
SensorManager sm;
sm = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
return Vals;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Vals[0]=(int)event.values[0];
Vals[1]=(int)event.values[1];
Vals[2]=(int)event.values[2];
}
}
Thanks .
Your context in AccelerometerClass is never set!
Possible solution:
public class AccelerometerClass implements SensorEventListener {
int[] Vals = new int[3];
public int[] AccelerometerInit(Context context) {
SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
return Vals;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
Vals[0] = (int) event.values[0];
Vals[1] = (int) event.values[1];
Vals[2] = (int) event.values[2];
}
}
in your Activity use
AccelerometerClass ac = new AccelerometerClass();
ac.AccelerometerInit(this);

Json Data on Fragment

I am learning android programming and created a basic project using android studio.
My application crashes when I click on the getdata button.
The MainActivity.java:
package com.cs_infotech.newpathshala;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ExampleFragment();
break;
case 1:
fragment = new Example1Fragment();
break;
case 2:
fragment = new Example2Fragment();
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
} }
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = "ePathshala";
break;
case 2:
mTitle = "Daily Collection";
break;
case 3:
mTitle = "New Enrolled";
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
test1.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="vertical" >
<Button
android:id="#+id/getdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:text="Get Data" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/getdata" />
<TextView
android:id="#+id/vers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/api"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Example1Fragment.java:
package com.cs_infotech.newpathshala;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.cs_infotech.newpathshala.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class Example1Fragment extends Fragment {
ListView list;
TextView ver;
TextView name;
TextView api;
Button btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://api.learn2crack.com/android/jsonos/";
//JSON Node Names
private static final String TAG_OS = "android";
private static final String TAG_VER = "ver";
private static final String TAG_NAME = "name";
private static final String TAG_API = "api";
JSONArray android = null;
private View v;
public Example1Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test1, container, false);
oslist = new ArrayList<HashMap<String, String>>();
btngetdata = (Button) rootView.findViewById(R.id.getdata);
v=rootView;
btngetdata.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new JSONParse().execute();
//Toast.makeText(getActivity(), "You Clicked at " , Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
name = (TextView) v.findViewById(R.id.name);
ver = (TextView) v.findViewById(R.id.vers);
api = (TextView) v.findViewById(R.id.api);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl("http://api.learn2crack.com/android/jsonos/");
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String ver = c.getString(TAG_VER);
String name = c.getString(TAG_NAME);
String api = c.getString(TAG_API);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_VER, ver);
map.put(TAG_NAME, name);
map.put(TAG_API, api);
oslist.add(map);
list=(ListView) v.findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_v,
new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
R.id.vers,R.id.name, R.id.api});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " + oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Logcat:
04-10 10:12:52.253 30126-30126/com.cs_infotech.newpathshala D/ActivityThread﹕ handleBindApplication:com.cs_infotech.newpathshala
04-10 10:12:52.343 30126-30126/com.cs_infotech.newpathshala W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-10 10:12:52.343 30126-30126/com.cs_infotech.newpathshala D/DisplayManager﹕ DisplayManager()
04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala W/dalvikvm﹕ VFY: unable to resolve virtual method 11359: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala W/dalvikvm﹕ VFY: unable to resolve virtual method 11365: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-10 10:12:52.433 30126-30126/com.cs_infotech.newpathshala I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
04-10 10:12:52.433 30126-30126/com.cs_infotech.newpathshala W/dalvikvm﹕ VFY: unable to resolve virtual method 9053: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
04-10 10:12:52.433 30126-30126/com.cs_infotech.newpathshala D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala W/dalvikvm﹕ VFY: unable to resolve virtual method 374: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala W/dalvikvm﹕ VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getType (I)I
04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-10 10:12:52.834 30126-30126/com.cs_infotech.newpathshala D/OpenGLRenderer﹕ Enabling debug mode 0
04-10 10:12:52.934 30126-30126/com.cs_infotech.newpathshala I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy#41e049e0 time:9101016
04-10 10:15:04.772 30126-30126/com.cs_infotech.newpathshala I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy#41e049e0 time:9232856
04-10 10:15:11.359 30126-30656/com.cs_infotech.newpathshala W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x416c8bc0)
04-10 10:15:11.379 30126-30656/com.cs_infotech.newpathshala E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.cs_infotech.newpathshala, PID: 30126
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.cs_infotech.newpathshala.JSONParser.getJSONFromUrl(JSONParser.java:44)
at com.cs_infotech.newpathshala.Example1Fragment$JSONParse.doInBackground(Example1Fragment.java:80)
at com.cs_infotech.newpathshala.Example1Fragment$JSONParse.doInBackground(Example1Fragment.java:62)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.getaddrinfo(Native Method)
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.cs_infotech.newpathshala.JSONParser.getJSONFromUrl(JSONParser.java:44)
            at com.cs_infotech.newpathshala.Example1Fragment$JSONParse.doInBackground(Example1Fragment.java:80)
            at com.cs_infotech.newpathshala.Example1Fragment$JSONParse.doInBackground(Example1Fragment.java:62)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
            at libcore.io.Posix.getaddrinfo(Native Method)
            at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.cs_infotech.newpathshala.JSONParser.getJSONFromUrl(JSONParser.java:44)
            at com.cs_infotech.newpathshala.Example1Fragment$JSONParse.doInBackground(Example1Fragment.java:80)
            at com.cs_infotech.newpathshala.Example1Fragment$JSONParse.doInBackground(Example1Fragment.java:62)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
04-10 10:15:12.330 30126-30126/com.cs_infotech.newpathshala E/OpenGLRenderer﹕ SFEffectCache:clear(), mSize = 0
04-10 10:15:12.360 30126-30126/com.cs_infotech.newpathshala E/WindowManager﹕ android.view.WindowLeaked: Activity com.cs_infotech.newpathshala.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41e91890 V.E..... R......D 0,0-471,144} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:388)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at com.cs_infotech.newpathshala.Example1Fragment$JSONParse.onPreExecute(Example1Fragment.java:74)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.cs_infotech.newpathshala.Example1Fragment$1.onClick(Example1Fragment.java:56)
at android.view.View.performClick(View.java:4508)
at android.view.View$PerformClick.run(View.java:18675)
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:5584)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
04-10 10:15:14.992 30126-30656/com.cs_infotech.newpathshala I/Process﹕ Sending signal. PID: 30126 SIG: 9
How can I resolve this?

Not able to run Jeremy Feinstein sliding menu on android version 2.3.3

I have implemented Sliding menu using Jeremy Feinstein library.
Which is working perfectly fine on Android version : 4.x.x
But when I try to execute on android version : 2.3.x, application crashes with following error.
11-29 19:25:52.628: E/AndroidRuntime(26836): FATAL EXCEPTION: main
11-29 19:25:52.628: E/AndroidRuntime(26836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.variouscal.calculators/com.variouscal.calculators.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.os.Looper.loop(Looper.java:130)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-29 19:25:52.628: E/AndroidRuntime(26836): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 19:25:52.628: E/AndroidRuntime(26836): at java.lang.reflect.Method.invoke(Method.java:507)
11-29 19:25:52.628: E/AndroidRuntime(26836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-29 19:25:52.628: E/AndroidRuntime(26836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-29 19:25:52.628: E/AndroidRuntime(26836): at dalvik.system.NativeStart.main(Native Method)
11-29 19:25:52.628: E/AndroidRuntime(26836): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-29 19:25:52.628: E/AndroidRuntime(26836): at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.setMenu(SlidingMenu.java:375)
11-29 19:25:52.628: E/AndroidRuntime(26836): at com.variouscal.calculators.MainActivity.onCreate(MainActivity.java:30)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-29 19:25:52.628: E/AndroidRuntime(26836): ... 11 more
11-29 19:25:52.628: E/AndroidRuntime(26836): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.variouscal.calculators-1.apk]
11-29 19:25:52.628: E/AndroidRuntime(26836): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-29 19:25:52.628: E/AndroidRuntime(26836): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-29 19:25:52.628: E/AndroidRuntime(26836): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
11-29 19:25:52.628: E/AndroidRuntime(26836): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-29 19:25:52.628: E/AndroidRuntime(26836): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
11-29 19:25:52.628: E/AndroidRuntime(26836): ... 18 more
Below is MainActivity.java and SlidingMenuFragment.java
MainActivity.java
package com.variouscal.calculators;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class MainActivity extends Activity {
private SlidingMenu slidingMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slidingMenu.setMenu(R.layout.slidingmenu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.slidingMenu.toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
if (slidingMenu.isMenuShowing()) {
slidingMenu.toggle();
} else {
super.onBackPressed();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
this.slidingMenu.toggle();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
SlidingMenuFragment.java
package com.variouscal.calculators;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
public class SlidingMenuFragment extends Fragment implements ExpandableListView.OnChildClickListener {
private ExpandableListView sectionListView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<Section> sectionList = createMenu();
View view = inflater.inflate(R.layout.slidingmenu_fragment, container, false);
this.sectionListView = (ExpandableListView) view.findViewById(R.id.slidingmenu_view);
this.sectionListView.setGroupIndicator(null);
SectionListAdapter sectionListAdapter = new SectionListAdapter(this.getActivity(), sectionList);
this.sectionListView.setAdapter(sectionListAdapter);
this.sectionListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
this.sectionListView.setOnChildClickListener(this);
int count = sectionListAdapter.getGroupCount();
for (int position = 0; position < count; position++) {
this.sectionListView.expandGroup(position);
}
return view;
}
private List<Section> createMenu() {
List<Section> sectionList = new ArrayList<Section>();
Section oDemoSection = new Section("Calculators");
oDemoSection.addSectionItem(101,"Love Calculator", "slidingmenu_friends");
oDemoSection.addSectionItem(102, "Age Calculator", "slidingmenu_airport");
oDemoSection.addSectionItem(103, "BMI Calculator", "slidingmenu_airport");
Section oGeneralSection = new Section("General");
oGeneralSection.addSectionItem(201, "Settings", "slidingmenu_settings");
oGeneralSection.addSectionItem(202, "Rate this app", "slidingmenu_rating");
oGeneralSection.addSectionItem(203, "Eula", "slidingmenu_eula");
oGeneralSection.addSectionItem(204, "Quit", "slidingmenu_quit");
sectionList.add(oDemoSection);
sectionList.add(oGeneralSection);
return sectionList;
}
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch ((int)id) {
case 101:
Intent intent = new Intent("com.variouscal.calculators.LoveCalculator");
startActivity(intent);
break;
case 102:
Intent intent1 = new Intent("com.variouscal.calculators.AgeCalculator");
startActivity(intent1);
break;
case 103:
Intent intent2 = new Intent("com.variouscal.calculators.BmiCalculator");
startActivity(intent2);
break;
case 202:
//TODO
break;
case 203:
//TODO
break;
case 204:
//TODO
break;
}
return false;
}
}
How do I make it work for Android version : 2.3.x.
Thanks
Try using the following code. This is how I did it and it worked for me. Hope this code helps you.
package com.example.calculators;
import android.os.Bundle;
import android.view.Menu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class MainActivity extends SlidingFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.slidingmenu);
getSlidingMenu().setMode(SlidingMenu.LEFT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSlidingMenu().setFadeDegree(0.35f);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is my slidingmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragback"
android:name="com.example.calculators.ListFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Not a supported ISO 4217 currency code: GBP

How to extract the currency symbol using currency which is like string "GBP" i am using
String s = obj.getCurrency().toString();
Currency currency=Currency.getInstance(s);
but it was throwing this exception :
01-31 11:40:40.464: E/AndroidRuntime(3929): FATAL EXCEPTION: main
01-31 11:40:40.464: E/AndroidRuntime(3929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.primesoft.games.potshot/com.primesoft.games.potshot.PotshotActivity}: java.lang.IllegalArgumentException: Not a supported ISO 4217 currency code: GBP
01-31 11:40:40.464: E/AndroidRuntime(3929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-31 11:40:40.464: E/AndroidRuntime(3929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-31 11:40:40.464: E/AndroidRuntime(3929): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-31 11:40:40.464: E/AndroidRuntime(3929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-31 11:40:40.464: E/AndroidRuntime(3929): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 11:40:40.464: E/AndroidRuntime(3929): at android.os.Looper.loop(Looper.java:130)
01-31 11:40:40.464: E/AndroidRuntime(3929): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-31 11:40:40.464: E/AndroidRuntime(3929): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 11:40:40.464: E/AndroidRuntime(3929): at java.lang.reflect.Method.invoke(Method.java:507)
01-31 11:40:40.464: E/AndroidRuntime(3929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

Categories