Android Application Subclass Across Activities - android

I'm running into a NullPointerException, and I can't seem to figure out why. I wouldn't say I'm that experienced in android, so it seems easy for me to get lost when debugging these... :(
I've got an Application subclass defined as follows:
public class UserData extends Application {
private String email, firstName, lastName;
public String getEmail() {
return email;
}
public void setEmail(String str) {
email = str;
}
}
In one activity (loginpage), I set the email string:
UserData user = (UserData)getApplication();
user.setEmail(results);
When I log getEmail(), I see the results in this activity just fine.
In another activity (dashboard), I get the email string as follows:
UserData user = (UserData)getApplication();
Log.d("Email: ", user.getEmail());
This time, user.getEmail() returns a nullpointer. Can someone point out why this is occurring? I've got the subclass defined in my Manifest like this:
<application
android:name=".UserData"></application>
Error Log:
-12 18:10:51.261 19471-19471/habitatfinal.habitatfinal E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: habitatfinal.habitatfinal, PID: 19471
java.lang.RuntimeException: Unable to start activity ComponentInfo{habitatfinal.habitatfinal/habitatfinal.habitatfinal.dashboard}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at habitatfinal.habitatfinal.dashboard.onCreate(dashboard.java:29)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

I found out what was going on. The problem is onPostExecute doesn't necessarily run after the onCreate() method finishes running. Because they are two different threads, the subclass may not be initialized before the program passes the intent into the next activity.
Therefore, the variables in the object weren't set before it hit the new Activity.

Related

InvocationTargetException : Could not execute method of the activity

I am new in Android and I want to put Click event on XML instead of java, and I have implemented following method on Activity class
public void onInfoClick(View v){
switch(v.getId()) {
case R.id.ivAcceptAmericanExpress:
showPopup(v, getString(R.string.apt_american_express));
break;
}
}
and in XML , I have done following code
<ImageView
android:id="#+id/ivAcceptAmericanExpress"
android:layout_width="24.286dp"
android:layout_height="24.286dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/txtAcceptAmericanExpress"
android:src="#drawable/info_image"
android:onClick="onInfoClick" />
but when I click on Image I am getting following error
Could not execute method of the activity
android.view.View$1.onClick(View.java:4021)
android.view.View.performClick(View.java:4781)
android.view.View$PerformClick.run(View.java:19873)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:135)
android.app.ActivityThread.main(ActivityThread.java:5291)
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699):: Cause : java.lang.reflect.InvocationTargetException
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
android.view.View$1.onClick(View.java:4016)
android.view.View.performClick(View.java:4781)
android.view.View$PerformClick.run(View.java:19873)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:135)
android.app.ActivityThread.main(ActivityThread.java:5291)
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
The InvocationTargetException usually points to an exception in the onClick method which you are trying to execute - in your case the onInfoClick method - you can confirm this by commenting out your case-statement and see if you still get the error. Once you have identified the problem - you can make the necessary changes to resolve the exception. You should probably surround the body of the method with a try-catch to be safe.

ParseTwitterUtils initialize throws NullPointerException

I am getting a NullPointerException from calling
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
Here is the stack trace
java.lang.RuntimeException: Unable to create application com.myapp.app.android.Application: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5015)
at android.app.ActivityThread.access$1600(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
at com.parse.Parse.getParseDir(Parse.java:313)
at com.parse.ParseCorePlugins.getCurrentUserController(ParseCorePlugins.java:124)
at com.parse.ParseUser.getCurrentUserController(ParseUser.java:56)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:892)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:879)
at com.parse.ParseUser.registerAuthenticationProvider(ParseUser.java:1125)
at com.parse.ParseTwitterUtils.getAuthenticationProvider(ParseTwitterUtils.java:20)
at com.parse.ParseTwitterUtils.initialize(ParseTwitterUtils.java:50)
at com.myapp.app.android.Application.onCreate(Application.java:42)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5012)
            at android.app.ActivityThread.access$1600(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
My app used to work before I added the line ParseTwitterUtils.initialize(...). Here is the onCreate method of my Application class
public void onCreate(){
super.onCreate();
registerDatabaseTables();
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
}
Unfortunately the instructions on parse.com do not say this, but you have to initialize Parse before you initialize Twitter. This is not obvious since you are required to do so much before you initialize parse. So anyway change your code to this:
public void onCreate(){
super.onCreate();
registerDatabaseTables();
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
}

Cloudmine java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory;

I am trying to store some data using Cloudmine.
However, I got the error java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory; when I call initialize() for authorization.
I'm not running on a separate thread or something. I'm testing stuff out right now so I just have it so that when a button is clicked, it uploads some information from some EditText fields.
This is my code
public class ButtonClick implements View.OnClickListener {
private MainWindowActivity mainWindowActivity;
public ButtonClick(MainWindowActivity mainWindowActivity, Button doneBtn, Button addBtn) {
this.mainWindowActivity = mainWindowActivity;
addBtn.setEnabled(true);
addBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// This will initialize your credentials
// ERROR HERE
CMApiCredentials initialize = CMApiCredentials.initialize("id", "key");
SimpleCMObject location = new SimpleCMObject();
location.add("address", Runner.getAddressStr());
location.add("city", Runner.getCityStr());
location.add("state", Runner.getStateStr());
location.add("zip", Runner.getZipStr());
location.save(new ObjectModificationResponseCallback() {
public void onCompletion(ObjectModificationResponse response) {
Toast.makeText(mainWindowActivity, "Location saved: " + response.wasSuccess(), Toast.LENGTH_SHORT).show();
}
});
EditText address = (EditText) mainWindowActivity.findViewById(R.id.address);
address.setText("");
EditText city = (EditText) mainWindowActivity.findViewById(R.id.city);
city.setText("");
EditText state = (EditText) mainWindowActivity.findViewById(R.id.state);
state.setText("");
EditText zip = (EditText) mainWindowActivity.findViewById(R.id.zip);
zip.setText("");
}
}
This is the Logcat:
Process: prog, PID: 26580
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory;
at com.cloudmine.api.CMApiCredentials.<clinit>(CMApiCredentials.java:21)
at prog.ButtonClick.onClick(ButtonClick.java:52)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.slf4j.LoggerFactory" on path: DexPathList[[zip file "/data/app/prog-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.cloudmine.api.CMApiCredentials.<clinit>(CMApiCredentials.java:21)
            at prog.ButtonClick.onClick(ButtonClick.java:52)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
As I have commented, download zip file from SLF4J distribution and use as library in your project. Then, use slf4j-api-1.7.12.jar (and slf4j-android-1.7.12.jar if needed).
I had the same error and found this solution on github.
Just add the following to your grandle:
implementation 'org.slf4j:slf4j-api:1.7.25'
https://github.com/nomis/slf4j-android
I hade the same problem only on Android 5.0 - 5.1 (a real device and emulators). At that moment, I used Android Studio 2.3.3 and build tools v26.
I solved the problem turning off Instant Run that I didn't use anyway.
Steps:
go to File -> Settings -> Build, Execution, Deployment -> Instant run
Disable it (first checkbox).

Try to add values to List<Integer>

when I tried to add some values to my List inside my onResume() method i got this error:
java.lang.RuntimeException: Unable to resume activity {.../...MainActivity}: java.lang.UnsupportedOperationException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3096)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3127)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2482)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5415)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at de.toeducate.tograde.MainActivity.onResume(MainActivity.java:151)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
at android.app.Activity.performResume(Activity.java:6237) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3085)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3127)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2482)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5415)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
and here is the code I wrote:
List<Integer> marks = Collections.emptyList();
for (int i = 0; i < dataBase.getDataMark().size(); i++) {
marks.add(dataBase.getDataMark().get(i).getValue());
}
What does this mean?
Collections.emptyList(); returns a List that you cannot add to (hence the UnsupportedOperationException).
Use
List<Integer> marks = new ArrayList<Integer>();
instead.
Collections.emptyList() returns an instance of AbstractList. This is meant by the line
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
in your exception's stack trace. This said you cannot call add on your marks list as add is not implemented.
Instead instantiate marks with a class that allows for adding elements, such as ArrayList:
List<Integer> marks = new ArrayList<Integer>();
Besides ArrayList, LinkedList is another implementation of AbstractList that supports adding elements. Most typically programmers use an ArrayList.

Android - Activity transition crash with ActionBarActivity

I'm currently trying to implement Material Design activity transitions. For a reason i really don't know, it crashes. Can someone give me a clue? (I don't know if it's important, but inside my MainActivity, i've a ViewPager with Fragments from which I try to start the next Activity)
Thanks a lot for your help.
My code:
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
(...)
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("TAAG", "MainActivity::onCreate1");
super.onCreate(savedInstanceState);
Log.i("TAAG", "MainActivity::onCreate2");
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Log.i("TAAG", "MainActivity::onCreate3");
setContentView(R.layout.activity_main);
Log.i("TAAG", "MainActivity::onCreate4");
(...)
}
Here are the logs:
11-13 18:12:36.442 1098-1098/I/TAAG﹕ MainActivity::onCreate1
11-13 18:12:36.544 1098-1098/I/TAAG﹕ MainActivity::onCreate2
11-13 18:12:36.559 1098-1098/E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mypackage, PID: 1098
java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:301)
at mypackage.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Try calling getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); before super.onCreate().

Categories

Resources