Google Maps android API, drawing polylines in a loop - java

I have two String arrays, latitude and longitude
I want to draw polylines on the map, the documentation says, that it's as easy as pie. And it is. If I add polylines manually like this, it works:
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0))
.add(new LatLng(37.45, -122.2))
But the problem starts when I'm trying to loop adding polylines:
rectOptions = new PolylineOptions();
for(int i = 0; i<latitude.length(); i++){
rectOptions.add(new LatLng(Long.getLong(latitude[i]), Long.getLong(longitude[i])));
// Log.d("coordinates", latitude[i]+"|"+longitude[i]);
}
I get a java.lang.NullPointerException And I don't know why. I know the problem is in the rectOptions.add() method, because Log.d("coordinates", latitude[i]+"|"+longitude[i]); works fine in the same loop.
Here's the output of LogCat:
01-02 11:59:27.380: E/AndroidRuntime(25146): FATAL EXCEPTION: main
01-02 11:59:27.380: E/AndroidRuntime(25146): java.lang.NullPointerException
01-02 11:59:27.380: E/AndroidRuntime(25146): at com.shniv.MainActivity$1.onClick(MainActivity.java:82)
01-02 11:59:27.380: E/AndroidRuntime(25146): at android.view.View.performClick(View.java:3519)
01-02 11:59:27.380: E/AndroidRuntime(25146): at android.view.View$PerformClick.run(View.java:14140)
01-02 11:59:27.380: E/AndroidRuntime(25146): at android.os.Handler.handleCallback(Handler.java:605)
01-02 11:59:27.380: E/AndroidRuntime(25146): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 11:59:27.380: E/AndroidRuntime(25146): at android.os.Looper.loop(Looper.java:137)
01-02 11:59:27.380: E/AndroidRuntime(25146): at android.app.ActivityThread.main(ActivityThread.java:4456)
01-02 11:59:27.380: E/AndroidRuntime(25146): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 11:59:27.380: E/AndroidRuntime(25146): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 11:59:27.380: E/AndroidRuntime(25146): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-02 11:59:27.380: E/AndroidRuntime(25146): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-02 11:59:27.380: E/AndroidRuntime(25146): at dalvik.system.NativeStart.main(Native Method)

Related

Trouble with ArrayList [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a trouble in my app retrieving my data from parse.com
I just wanted to recover data from website in an ArrayList made with a custom class, for using later in my app
Here is code but I can't test it on my emulator because my app crashes on boot, despite I don't have coding errors
PS: I'm very new in android coding, so any help is well accepted!
final int duration = Toast.LENGTH_SHORT;
ParseQuery<ParseObject> queryP = ParseQuery.getQuery("Menu");
queryP.orderByAscending("Id");
queryP.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> MenuList, ParseException e) {
int Id_c;
String Image_c;
String Name_c;
String Description_c;
String Type_c;
Double Cost_c;
ArrayList<Plate> FullMenu = new ArrayList<Plate>();
Plate plate;
if (e == null) {
for (ParseObject plate_o : MenuList) {
Id_c = plate_o.getInt("Id");
Image_c = plate_o.getString("Image");
Name_c = plate_o.getString("Name");
Description_c = plate_o.getString("Description");
Type_c = plate_o.getString("Type");
Cost_c = plate_o.getDouble("Cost");
String text = Name_c;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
FullMenu.add(Id_c, new Plate(Id_c, Image_c, Name_c, Description_c, Type_c, Cost_c));
plate = FullMenu.get(Id_c);
if (plate.getId() % 5 == 0) {
text = plate.getName();
toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
}
}
else {
String text = "The get request failed.";
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
}
});
And here is my logcat
01-02 19:18:58.916 4406-4406/mycompany.restaurantapp I/art: Not late-enabling -Xcheck:jni (already on)
01-02 19:18:58.916 4406-4406/mycompany.restaurantapp I/art: Late-enabling JIT
01-02 19:18:58.918 4406-4406/mycompany.restaurantapp I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
01-02 19:18:58.962 4406-4406/mycompany.restaurantapp W/System: ClassLoader referenced unknown path: /data/app/mycompany.restaurantapp-2/lib/x86
01-02 19:18:59.401 4406-4438/mycompany.restaurantapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-02 19:18:59.446 4406-4406/mycompany.restaurantapp D/: HostConnection::get() New Host Connection established 0xa335d700, tid 4406
01-02 19:18:59.542 4406-4438/mycompany.restaurantapp D/: HostConnection::get() New Host Connection established 0xa335d8d0, tid 4438
01-02 19:18:59.556 4406-4438/mycompany.restaurantapp I/OpenGLRenderer: Initialized EGL, version 1.4
01-02 19:18:59.703 4406-4438/mycompany.restaurantapp W/EGL_emulation: eglSurfaceAttrib not implemented
01-02 19:18:59.704 4406-4438/mycompany.restaurantapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad79f160, error=EGL_SUCCESS
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp D/AndroidRuntime: Shutting down VM
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: Process: mycompany.restaurantapp, PID: 4406
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at java.util.ArrayList.add(ArrayList.java:147)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at mycompany.restaurantapp.MainActivity$1.done(MainActivity.java:91)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at mycompany.restaurantapp.MainActivity$1.done(MainActivity.java:69)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-02 19:19:11.067 4406-4406/? I/Process: Sending signal. PID: 4406 SIG: 9
FullMenu.add(Id_c, new Plate(Id_c, Image_c, Name_c, Description_c, Type_c, Cost_c));
The wrong line. You're trying to add the element to empty ArrayList at certain position. Try to make it like this
FullMenu.add(new Plate(Id_c, Image_c, Name_c, Description_c, Type_c, Cost_c));
If you want to have an access by id as key you should use Map structure.

Null Pointer Exception while setting value of TextView in an Array Adapter class - Android

I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file

NullPointerException at if (runPrevious.equals("yes")) {

I'm trying to setup a method of executing one task if my application has been run previously - and another task if it has not.
To do this I have created a String runPrevious:
My problem is every time I attempt to execute the application I get a force close due to a null pointer exception here:
if (runPrevious.equals("yes")) {
which I don't understand - since I'm declaring the value of the String here:
String runPrevious = "No";
LOGCAT:
12-13 13:20:12.485: E/AndroidRuntime(1803): FATAL EXCEPTION: main
12-13 13:20:12.485: E/AndroidRuntime(1803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idg.omv/com.idg.omv.ui.phone.Home}: java.lang.NullPointerException
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.access$600(ActivityThread.java:156)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.os.Looper.loop(Looper.java:153)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.main(ActivityThread.java:5297)
12-13 13:20:12.485: E/AndroidRuntime(1803): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 13:20:12.485: E/AndroidRuntime(1803): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-13 13:20:12.485: E/AndroidRuntime(1803): at dalvik.system.NativeStart.main(Native Method)
12-13 13:20:12.485: E/AndroidRuntime(1803): Caused by: java.lang.NullPointerException
12-13 13:20:12.485: E/AndroidRuntime(1803): at com.idg.omv.ui.phone.Home.onCreate(Home.java:113)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.Activity.performCreate(Activity.java:5262)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-13 13:20:12.485: E/AndroidRuntime(1803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-13 13:20:12.485: E/AndroidRuntime(1803): ... 11 more
12-13 13:20:13.969: I/Process(1803): Sending signal. PID: 1803 SIG: 9
JAVA
public class Home extends YouTubeBaseActivity implements
VideoClickListener {
String runPrevious = "No";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
String runPrevious = getIntent().getStringExtra("runPrevious");
if (runPrevious.equals("yes")) {
String playlist = getIntent().getStringExtra("playlist");
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
}else{
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
}
You are declaring it once again (probably with null value)
String runPrevious = getIntent().getStringExtra("runPrevious");
do not test strings for null if you are comparing them to a literal value. Instead use non-null-pointer succeptible compairsons.
In your case:
if ("yes".equals(runPrevious))
This will never throw a null pointer exception.

Spinner problems with Sharedpreferences

Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.

Google Maps Android API v2 ClassNotFound Runtime error

EDIT: For those struggling with this, checkout this link. Follow it carefully, but it does work! The crucial part is using keytool to generate your SHA1 hash against your debug keystore. Also, don't forget to add one for release too!
I've been trying to setup Google Maps Android API v2 and after setting everything up as said I'm getting this runtime exception ClassNotFound:
01-02 16:29:07.927: E/AndroidRuntime(12508): FATAL EXCEPTION: main
01-02 16:29:07.927: E/AndroidRuntime(12508): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.os.Looper.loop(Looper.java:130)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.ActivityThread.main(ActivityThread.java:3701)
01-02 16:29:07.927: E/AndroidRuntime(12508): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 16:29:07.927: E/AndroidRuntime(12508): at java.lang.reflect.Method.invoke(Method.java:507)
01-02 16:29:07.927: E/AndroidRuntime(12508): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-02 16:29:07.927: E/AndroidRuntime(12508): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-02 16:29:07.927: E/AndroidRuntime(12508): at dalvik.system.NativeStart.main(Native Method)
01-02 16:29:07.927: E/AndroidRuntime(12508): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-02 16:29:07.927: E/AndroidRuntime(12508): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.Activity.setContentView(Activity.java:1657)
01-02 16:29:07.927: E/AndroidRuntime(12508): at com.example.app.MapActivity.onCreate(MapActivity.java:11)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
01-02 16:29:07.927: E/AndroidRuntime(12508): ... 11 more
01-02 16:29:07.927: E/AndroidRuntime(12508): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.app-1.apk]
01-02 16:29:07.927: E/AndroidRuntime(12508): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-02 16:29:07.927: E/AndroidRuntime(12508): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-02 16:29:07.927: E/AndroidRuntime(12508): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
01-02 16:29:07.927: E/AndroidRuntime(12508): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-02 16:29:07.927: E/AndroidRuntime(12508): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
01-02 16:29:07.927: E/AndroidRuntime(12508): ... 20 more
Has anyone ever come across this before?
XML:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
MapActivity:
import android.os.Bundle;
import android.app.Activity;
public class MapActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
}
Your Activity needs to extend FragmentActivity from the support library
Try this code working perfect help full to you.
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=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Java
package com.example.androidmapsv2;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment
= (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}
}
Did you run on AVD? if yes try on real devices

Categories