Geocoder initialization fails - java

I am getting a NullPointerException when trying to declare the Geocoder in my application. I have the following declaration :
public class MainActivity extends Activity {
private Geocoder geocoder = new Geocoder(this, Locale.getDefault());
...
}
I get the following LogCat :
03-20 10:48:55.729: D/AndroidRuntime(604): Shutting down VM
03-20 10:48:55.729: W/dalvikvm(604): threadid=1: thread exiting with uncaught exception
(group=0x40a71930)
03-20 10:48:56.209: E/AndroidRuntime(604): FATAL EXCEPTION: main
03-20 10:48:56.209: E/AndroidRuntime(604): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.coord/com.example.coord.MainActivity}: java.lang.NullPointerException
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.os.Looper.loop(Looper.java:137)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 10:48:56.209: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 10:48:56.209: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 10:48:56.209: E/AndroidRuntime(604): at dalvik.system.NativeStart.main(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604): Caused by: java.lang.NullPointerException
03-20 10:48:56.209: E/AndroidRuntime(604): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
03-20 10:48:56.209: E/AndroidRuntime(604): at com.example.coord.MainActivity.<init>(MainActivity.java:21)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.Class.newInstanceImpl(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604): at java.lang.Class.newInstance(Class.java:1319)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-20 10:48:56.209: E/AndroidRuntime(604): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
Line 21 is my Geocoder declaration. What is wrong with my code?

The context is only available when the activity is started so you cannot initialize the geocoder in the class body. Try to initialize it in the onCreate or onResume method instead...
public class MainActivity extends Activity {
private Geocoder mGeocoder;
#Override
protected void onCreate(Bundle _icicle) {
super.onCreate(_icicle);
mGeocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
}
}

Add this permissions in to manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
and use getApplicationContext() insted of this

It is recommended to use the GeoCode in Background or on separate thread as defined by Google Developers page.
new Thread(new Runnable()
{
#Override
public void run()
{
//Do things.
Geocoder geocoder = new Geocoder(getBaseContext());
try {
// Getting a maximum of 5 Address that matches the input text
addresses = geocoder.getFromLocationName(addressText,5);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

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;

Android: Class not found exception

I've looked all similar questions and none of the solutions presented did solve my problem.
When my application is launcher I am presented with the following:
12-23 19:27:39.207: E/Trace(1884): error opening trace file: No such file or directory (2)
12-23 19:27:39.217: D/AndroidRuntime(1884): Shutting down VM
12-23 19:27:39.217: W/dalvikvm(1884): threadid=1: thread exiting with uncaught exception (group=0x416ae300)
12-23 19:27:39.217: E/AndroidRuntime(1884): FATAL EXCEPTION: main
12-23 19:27:39.217: E/AndroidRuntime(1884): java.lang.RuntimeException: Unable to instantiate application com.triandria.socialgeek.SocialGeek: java.lang.ClassNotFoundException: com.triandria.socialgeek.SocialGeek
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4124)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.app.ActivityThread.access$1300(ActivityThread.java:130)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.os.Looper.loop(Looper.java:137)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-23 19:27:39.217: E/AndroidRuntime(1884): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 19:27:39.217: E/AndroidRuntime(1884): at java.lang.reflect.Method.invoke(Method.java:511)
12-23 19:27:39.217: E/AndroidRuntime(1884): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-23 19:27:39.217: E/AndroidRuntime(1884): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-23 19:27:39.217: E/AndroidRuntime(1884): at dalvik.system.NativeStart.main(Native Method)
12-23 19:27:39.217: E/AndroidRuntime(1884): Caused by: java.lang.ClassNotFoundException: com.triandria.socialgeek.SocialGeek
12-23 19:27:39.217: E/AndroidRuntime(1884): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-23 19:27:39.217: E/AndroidRuntime(1884): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-23 19:27:39.217: E/AndroidRuntime(1884): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.app.Instrumentation.newApplication(Instrumentation.java:967)
12-23 19:27:39.217: E/AndroidRuntime(1884): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
12-23 19:27:39.217: E/AndroidRuntime(1884): ... 11 more
My class that extends Application
public class SocialGeek extends Application {
private static final String TAG = "Social Geek";
private static Context context;
public static SharedPreferences sharedPreferences;
private static SQLiteDatabase savedProfilesDatabase;
private static DatabaseHelper savedProfilesDatabaseHelper;
private static int width, height;
private static boolean REMOTE_PROFILE_CREATED = false;
private static Location CURRENT_USER_LOCATION;
#Override
public void onCreate() {
super.onCreate();
SocialGeek.context = getApplicationContext();
sharedPreferences = getSharedPreferences(Activity_my_profile.FILENAME,
MODE_PRIVATE);
calculateScreenDimensions();
//TODO if no memory card is present we get errors
savedProfilesDatabaseHelper = new DatabaseHelper(context);
ContentValues contentValues = new ContentValues();
contentValues.put(
SavedProfilesDatabaseScheme.SavedProfilesTable.PROFILE_NAME,
"Dobrovnik");
contentValues
.put(SavedProfilesDatabaseScheme.SavedProfilesTable.PROFILE_DESCRIPTION,
" Stolischnaya University");
savedProfilesDatabase = savedProfilesDatabaseHelper
.getWritableDatabase();// only for mock data
// int rowsDeleted =
// savedProfilesDatabase.delete(SavedProfilesTable.TABLE_NAME, null,
// null);// duplicate values? mock
// Log.d(TAG, "Rows deleted: "+rowsDeleted);
Long newRowId = savedProfilesDatabase.insert(
SavedProfilesTable.TABLE_NAME, null, contentValues);
Log.d(TAG, ("Inserted row: " + newRowId.toString()));
savedProfilesDatabase.close();
}
`
UPDATE: Seems like the problem is not in the libraries or any particular class, even if I remove the SocialGeek extends Application I got the same exception in my MainActivity. I removed all my dependencies, deleted even eclipse and re-installed but the problem remains...
Do you have package com.triandria.socialgeek; on the first line in java file?
Took me a while to figure this out.
I had to right click the project -> build path-> configure build path -> Source tab
and un-tick the Allow output folders for source folders.

not able to read back protobuff from file

i am writing a protobuf into file and then reading again as follows
protobuff block
Person personOne =
Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe#example.com")
.addPhone(
Person.PhoneNumber.newBuilder()
.setNumber("555-4321")
.setType(Person.PhoneType.HOME))
.build();
writing to file , here data is byte[] and parameter is personOne.toByteArray()
try {
FileOutputStream output = new FileOutputStream(file);
output.write(data);
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reading from file , but while reading i am getting error
try {
FileInputStream input = new FileInputStream(file);
byte [] buffer = new byte[input.read()];
input.read(buffer);
input.close();
return buffer;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
error
03-20 21:04:39.060: W/System.err(18074): com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
03-20 21:04:39.090: W/System.err(18074): at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
03-20 21:04:39.110: W/System.err(18074): at com.google.protobuf.GeneratedMessage.parseUnknownField(GeneratedMessage.java:193)
03-20 21:04:39.110: W/System.err(18074): at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:124)
03-20 21:04:39.110: W/System.err(18074): at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:107)
03-20 21:04:39.110: W/System.err(18074): at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:186)
03-20 21:04:39.110: W/System.err(18074): at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:1)
03-20 21:04:39.110: W/System.err(18074): at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:141)
03-20 21:04:39.110: W/System.err(18074): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:176)
03-20 21:04:39.110: W/System.err(18074): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:188)
03-20 21:04:39.110: W/System.err(18074): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:193)
03-20 21:04:39.110: W/System.err(18074): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
03-20 21:04:39.120: W/System.err(18074): at com.example.protodemo.AddressBookProtos$Person.parseFrom(AddressBookProtos.java:1088)
03-20 21:04:39.120: W/System.err(18074): at com.example.protodemo.MainActivity.onCreate(MainActivity.java:63)
03-20 21:04:39.120: W/System.err(18074): at android.app.Activity.performCreate(Activity.java:4470)
03-20 21:04:39.120: W/System.err(18074): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
03-20 21:04:39.120: W/System.err(18074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
03-20 21:04:39.120: W/System.err(18074): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-20 21:04:39.120: W/System.err(18074): at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-20 21:04:39.120: W/System.err(18074): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
03-20 21:04:39.120: W/System.err(18074): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 21:04:39.120: W/System.err(18074): at android.os.Looper.loop(Looper.java:137)
03-20 21:04:39.120: W/System.err(18074): at android.app.ActivityThread.main(ActivityThread.java:4517)
03-20 21:04:39.120: W/System.err(18074): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 21:04:39.120: W/System.err(18074): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 21:04:39.130: W/System.err(18074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-20 21:04:39.130: W/System.err(18074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-20 21:04:39.130: W/System.err(18074): at dalvik.system.NativeStart.main(Native Method)
Update
previously i was storing data into data.txt file but now i have removed the exception and getting new error
03-20 21:25:58.920: W/System.err(18470): com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
03-20 21:25:58.920: W/System.err(18470): at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
03-20 21:25:58.920: W/System.err(18470): at com.google.protobuf.GeneratedMessage.parseUnknownField(GeneratedMessage.java:193)
03-20 21:25:58.920: W/System.err(18470): at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:124)
03-20 21:25:58.920: W/System.err(18470): at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:107)
03-20 21:25:58.930: W/System.err(18470): at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:186)
03-20 21:25:58.930: W/System.err(18470): at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:1)
03-20 21:25:58.930: W/System.err(18470): at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:141)
03-20 21:25:58.930: W/System.err(18470): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:176)
03-20 21:25:58.930: W/System.err(18470): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:188)
03-20 21:25:58.930: W/System.err(18470): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:193)
03-20 21:25:58.930: W/System.err(18470): at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
03-20 21:25:58.930: W/System.err(18470): at com.example.protodemo.AddressBookProtos$Person.parseFrom(AddressBookProtos.java:1088)
03-20 21:25:58.930: W/System.err(18470): at com.example.protodemo.MainActivity.onCreate(MainActivity.java:63)
03-20 21:25:58.930: W/System.err(18470): at android.app.Activity.performCreate(Activity.java:4470)
03-20 21:25:58.930: W/System.err(18470): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
03-20 21:25:58.930: W/System.err(18470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
03-20 21:25:58.930: W/System.err(18470): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-20 21:25:58.940: W/System.err(18470): at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-20 21:25:58.940: W/System.err(18470): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
03-20 21:25:58.940: W/System.err(18470): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 21:25:58.940: W/System.err(18470): at android.os.Looper.loop(Looper.java:137)
03-20 21:25:58.940: W/System.err(18470): at android.app.ActivityThread.main(ActivityThread.java:4517)
03-20 21:25:58.940: W/System.err(18470): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 21:25:58.940: W/System.err(18470): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 21:25:58.940: W/System.err(18470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-20 21:25:58.940: W/System.err(18470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-20 21:25:58.940: W/System.err(18470): at dalvik.system.NativeStart.main(Native Method)
From your code:
byte [] buffer = new byte[input.read()];
Here you appear to be reading the first byte of the input and using it as the size of the buffer. However, when you wrote the file, you didn't actually write a size in the first byte; you only wrote the data:
FileOutputStream output = new FileOutputStream(file);
output.write(data);
output.close();
Instead of reading the file content to a byte[], just pass the input stream itself to parseFrom, like:
FileInputStream input = new FileInputStream(file);
Person person = Person.parseFrom(input);
If you must read into a byte array, then allocate the array according to the actual size of the file, e.g. using File#length(). Or, actually write the message size before writing the message. (Don't write it as just a single byte, though, because then you'll have problems if your message is larger than 255 bytes.)

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.

Dialog crashing when TextView set

I have the following code which is called by the onClick event of buttons contained within a ListView.
public void viewExerciseHistory(View view) {
// History button click handler
LinearLayout parentRow = (LinearLayout)view.getParent();
TextView exerciseNameTextView = (TextView)parentRow.getChildAt(0);
String exerciseName = (String)exerciseNameTextView.getText();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.exercise_history_dialog);
dialog.setTitle(exerciseName);
TextView headerTextView = (TextView)view.findViewById(R.id.exercise_history_dialog_name_textview);
//headerTextView.setText("History");
Button closeDialogButton = (Button)view.findViewById(R.id.exercise_history_dialog_close_button);
//closeDialogButton.setOnClickListener(new OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
// }
//});
dialog.show();
}
The ListView row is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/exercise_history_dialog_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="15sp" />
<Button android:id="#+id/exercise_history_dialog_close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:typeface="monospace"
android:textSize="12sp"
android:textColor="#color/button_blue_text"
android:text="#string/button_text_close"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
The above code works fine and displays the dialog, but if uncomment out the headerTextView.setText("History") or the closeDialog.setOnClickListener it crashes with a NullPointerException.
Can anyone explain what I'm doing wrong?
EDIT LogCat:
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:19.915: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.945: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:20.435: D/AndroidRuntime(26167): Shutting down VM
10-14 21:36:20.435: W/dalvikvm(26167): threadid=1: thread exiting with uncaught exception (group=0x40e27930)
10-14 21:36:20.445: E/AndroidRuntime(26167): FATAL EXCEPTION: main
10-14 21:36:20.445: E/AndroidRuntime(26167): java.lang.IllegalStateException: Could not execute method of the activity
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3599)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View.performClick(View.java:4204)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$PerformClick.run(View.java:17355)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.handleCallback(Handler.java:725)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Looper.loop(Looper.java:137)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 21:36:20.445: E/AndroidRuntime(26167): at dalvik.system.NativeStart.main(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.reflect.InvocationTargetException
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3594)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 11 more
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.NullPointerException
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.example.workoutlog.ManageWorkouts.viewExerciseHistory(ManageWorkouts.java:269)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 14 more
It's crashing because this line:
TextView headerTextView = (TextView) findViewById(R.id.exercise_history_dialog_name_textview);
is actually returning null.
The findViewById method is looking for the view on the main activity's content view. Instead you want to use the findViewById method on the view object. Calling findViewById on the view object will allow you to get the fields you are after.
dialog.setContentView(R.layout.exercise_history_dialog);
TextView headerTextView = (TextView) dialog.findViewById(R.id.exercise_history_dialog_name_textview);
headerTextView.setText("History");
Try changing these
TextView headerTextView = (TextView)findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)findViewById(R.id.exercise_history_dialog_close_button);
to
TextView headerTextView = (TextView)dialog.findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)dialog.findViewById(R.id.exercise_history_dialog_close_button);
You have to tell which view you want to findViewById from

Categories