Why cannot convert from string to int? - android

It should be something easy to do, but these commands return error when I try to convert a String to integer:
Intent recibir = getIntent();
String nombre = recibir.getStringExtra("hora_inicio");
int numero=0;
try {
numero = Integer.parseInt(nombre);
} catch(NumberFormatException nfe) {
nfe.printStackTrace();
}
Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();
Android monitor returns this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cristobal.policlinica, PID: 11025
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.cristobal.policlinica/com.example.cristobal.policlinica.CalendarActivity}:
android.content.res.Resources$NotFoundException: String resource ID
0x9
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.res.Resources$NotFoundException: String
resource 0x9
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.Toast.makeText(Toast.java:286)
at
com.example.cristobal.policlinica.CalendarActivity.onCreate(CalendarActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6237)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
thanks in advance

The problem is not with your conversion but with your Toast.makeText. When you call Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();
It considers numero as resId and tries to find the relative string resource in strings.xml which fails giving error:
Resources$NotFoundException: String resource ID
Instead of
Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();
DO
Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();

Try this:
number = Integer.parseInt(YourStringName.toString());

Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();

Related

Unable to use GetIntExtra integer value in for calculation

MainClass.java
Intent i;
i = new Intent(v.getContext(), Next.class);
i.putExtra("increment1",1);
v.getContext().startActivity(i);
Next.java
Intent mIntent = getIntent();
int age=mIntent.getIntExtra("increment1", 0);
String age1=Integer.toString(age);
Toast.makeText(this,age1, Toast.LENGTH_LONG).show();
When I use variable age in my toast I'm getting below error. That means, I can't use Integer value. Please help me to use the Integer value.
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.myapplication/test.myapplication.Next}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5896)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:380)
at android.widget.TextView.setText(TextView.java:4564)
at test.myapplication.Next.onCreate(Next.java:28)
at android.app.Activity.performCreate(Activity.java:6298)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5896) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

Pushwoosh.getInstance() error

I am getting an error when I try to update my pushwoosh applications in the Pushwoosh library.I've updated it in Gradle.
07-06 18:21:59.637 19950-19950/com.ahmet.sen E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ahmet.sen, PID: 19950
java.lang.ExceptionInInitializerError
at com.ahmet.sen.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.pushwoosh.notification.j com.pushwoosh.c.d()' on a null object reference
at com.pushwoosh.Pushwoosh.<init>(Unknown Source)
at com.pushwoosh.Pushwoosh.<clinit>(Unknown Source)
at com.ahmet.sen.MainActivity.onCreate(MainActivity.java:50) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Where i get the error
Pushwoosh.getInstance().registerForPushNotifications();
Duplicating response from Pushwoosh Github here:
This is not a bug of Pushwoosh SDK. This is a peculiarity of multi-processor applications in Android apps environment.
When you integrate analytics tools such as LeakCanary, AppMetrica, and others, these libraries start a new process for itself. A new instance of the app is created In this process. But you do not need to listen for pushes in this separate process, so by default you should not call registerForPushNotifications() inside Application.onCreate().
Therefore there are 2 possible solutions:
Not to call registerForPushNotifications inside Application.onCreate()
If you want to call registerForPushNotifications inside Application.onCreate(), you should perform a check in application's main process:
List < ActivityManager.RunningAppProcessInfo > runningAppProcesses = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
if (runningAppProcesses != null && runningAppProcesses.size() != 0) {
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo: runningAppProcesses) {
boolean isCurrentProcess = runningAppProcessInfo.pid == android.os.Process.myPid();
boolean isMainProcessName = getPackageName().equals(runningAppProcessInfo.processName);
if (isCurrentProcess && isMainProcessName) {
Pushwoosh.getInstance().registerForPushNotifications(...);
break;
}
}
}

Android: Using 2 ArrayAdapters Error

I now get a new error.
This is my code:
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int i, long id) {
Toast.makeText(getApplicationContext(), beaconAdap.getItem(i).toString() + " Added to history", Toast.LENGTH_SHORT).show();
historyBeacons.add(beaconAdap.getItem(i).toString());
Intent intent = new Intent(getBaseContext(), HistoryActivity.class);
intent.putStringArrayListExtra("beacons", historyBeacons);
startActivity(intent);
return true;
}
});
}
So i add the int i to an arraylist, i have tested that it goes in and it does. Then a start a new intent for the history activity and send the arraylist over.
This is the code in history:
ListView historyLv = (ListView) findViewById(R.id.historyView);
ArrayAdapter historyAdap;
ArrayList<String> historyBeacons = getIntent().getExtras().getStringArrayList("beacons");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
historyAdap = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, historyBeacons);
historyLv.setAdapter(historyAdap);
}
But i get this error:
03-18 17:44:41.238 18641-18641/nsa.com.museum E/AndroidRuntime: FATAL EXCEPTION: main
Process: nsa.com.museum, PID: 18641
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nsa.com.museum/nsa.com.museum.HistoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at nsa.com.museum.HistoryActivity.<init>(HistoryActivity.java:12)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
03-18 17:44:43.362 18846-18846/nsa.com.museum W/System: ClassLoader referenced unknown path: /data/app/nsa.com.museum-2/lib/x86_64
can You show the erro please
and I guess you forget to add
ListAdapter.notifyDataSetChanged();
after you add value to lisy
.........................................................
NullPointerException
this erro shows in Android cause you may not Initialized the Adapter
make sure you Initialized the 2nd adapter

realm null object reference

when i start my app with out network and call my data in my database app crash and this is my code
public void getAllMovies() {
findData = realm.where(MovieDb.class).findAll();
movieArrayList = new ArrayList(findData);
movieMethod();
}
and i make my conflagration in here
public class myApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
RealmConfiguration configuration = new RealmConfiguration
.Builder(this).name("Movie_database.realm").build();
Realm.setDefaultConfiguration(configuration);}}
and moviedb is my realm object
my app crash in line
findData = realm.where(MovieDb.class).findAll();
and this is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.massive.movieapp, PID: 4222
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.massive.movieapp/com.massive.movieapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
at com.massive.movieapp.FragmentForActivity.getAllMovies(FragmentForActivity.java:143)
at com.massive.movieapp.FragmentForActivity.CallNetwork(FragmentForActivity.java:62)
at com.massive.movieapp.FragmentForActivity.onCreate(FragmentForActivity.java:55)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method
The exception happens because your realm variable is null when you call the getAllMovies method.
Remember to call realm = Realm.getDefaultInstance(); previously.

Index 0 requested, with a size of 0 MyHelper Java Error

I know this problem so many times asked. and i read every post that made by others here but i cant really solve this problem on my case.
Please help me why this happen and how to fix it ?
this is my code :
public StoreData getStore(String storeid) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where "
+ COL_STORE_ID + "=?", new String[]{storeid});
StoreData data;
System.out.println("This is the problem");
c.moveToFirst();
data = new StoreData(
c.getString(c.getColumnIndex(COL_STORE_ID)),
c.getString(c.getColumnIndex(COL_ACCOUNT_ID)),
c.getString(c.getColumnIndex(COL_KOTA_ID)),
c.getString(c.getColumnIndex(COL_STORE_NAME)),
c.getString(c.getColumnIndex(COL_STORE_MANAGER)),
c.getString(c.getColumnIndex(COL_STORE_ADDRESS)),
c.getString(c.getColumnIndex(COL_STORE_TELEPHONE)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LAT)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LONG)),
c.getString(c.getColumnIndex(COL_STORE_LEADTIME)),
c.getString(c.getColumnIndex(COL_STORE_MD)));
db.close();
c.close();
return data;
}
this is my log :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.userinarts.mobilemerchpre/com.apps.userinarts.MobileMerchPre.StoresCheckinActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.apps.userinarts.MobileMerchPre.db.StoreHelper.getStore(StoreHelper.java:130)
at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.inCaseOffline(StoresCheckinActivity.java:90)
at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.onCreate(StoresCheckinActivity.java:83)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
looks like your query returned 0 results. To check it, don't ignore result of moveToFirst(). It will return false if cursor size is 0.
Do something like this:
if(c.moveToFirst()) {
data = new StoreData(
c.getString(c.getColumnIndex(COL_STORE_ID)),
c.getString(c.getColumnIndex(COL_ACCOUNT_ID)),
c.getString(c.getColumnIndex(COL_KOTA_ID)),
c.getString(c.getColumnIndex(COL_STORE_NAME)),
c.getString(c.getColumnIndex(COL_STORE_MANAGER)),
c.getString(c.getColumnIndex(COL_STORE_ADDRESS)),
c.getString(c.getColumnIndex(COL_STORE_TELEPHONE)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LAT)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LONG)),
c.getString(c.getColumnIndex(COL_STORE_LEADTIME)),
c.getString(c.getColumnIndex(COL_STORE_MD)));
db.close();
c.close();
}
return data;

Categories

Resources