Java Question Starting Activity from Library Null Pointer Error

bloxa69

Active Member
Licensed User
Longtime User
Hello everybody,
I'm having problems starting an activity defined inside a library. I've tried all methods I could find on the forum but none worked. So I've created a very simple activity, compiled it to a library and tried to start that activity from withing B4A, getting same problem - null pointer exception.

Library code:
B4X:
package com.helloword;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import android.content.Intent;

@ActivityObject
@Author("HelloWord")
@Version(1.0f)

@ShortName("HelloWord")
public class HelloWord extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

        TextView text = new TextView(this);
        text.setText("Hello World of Android! - Greetings from Java Code Geeks");
        setContentView(text);
   }

}

I've tried to start that activity from B4A in many different ways, including a helper class in the library but nothing works, here is one of them, B4A code (the library is referenced form within B4a, and activity is defined in manifest when compiling, no errors while compiling):

B4X:
      Dim i As Intent
      i.Initialize("", "")
      i.SetComponent("com.helloword/.HelloWord")
      StartActivity(i)

Manifest code:

B4X:
AddApplicationText(<activity android:name="com.helloword.HelloWord" />)


Error code:

B4X:
I/B4A     ( 2963): ~i:** Activity (main) Create, isFirst = true **
I/B4A     ( 2963): ~e:main_activity_create (java line: 244)
I/B4A     ( 2963): ~e:java.lang.NullPointerException
I/B4A     ( 2963): ~e:   at android.content.ComponentName.<init>(ComponentName.java:75)
I/B4A     ( 2963): ~e:   at android.content.Intent.<init>(Intent.java:3301)
I/B4A     ( 2963): ~e:   at com.helloword.HelloWord$HWHelper.GetMyIntent(HelloWord.java:33)
I/B4A     ( 2963): ~e:   at com.glwallpaper001.main._activity_create(main.java:244)
I/B4A     ( 2963): ~e:   at java.lang.reflect.Method.invokeNative(Native Method)
I/B4A     ( 2963): ~e:   at java.lang.reflect.Method.invoke(Method.java:511)
I/B4A     ( 2963): ~e:   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
I/B4A     ( 2963): ~e:   at com.glwallpaper001.main.afterFirstLayout(main.java:89)
I/B4A     ( 2963): ~e:   at com.glwallpaper001.main.access$100(main.java:16)
I/B4A     ( 2963): ~e:   at com.glwallpaper001.main$WaitForLayout.run(main.java:74)
I/B4A     ( 2963): ~e:   at android.os.Handler.handleCallback(Handler.java:615)
I/B4A     ( 2963): ~e:   at android.os.Handler.dispatchMessage(Handler.java:92)
I/B4A     ( 2963): ~e:   at android.os.Looper.loop(Looper.java:137)
I/B4A     ( 2963): ~e:   at android.app.ActivityThread.main(ActivityThread.java:4745)
I/B4A     ( 2963): ~e:   at java.lang.reflect.Method.invokeNative(Native Method)
I/B4A     ( 2963): ~e:   at java.lang.reflect.Method.invoke(Method.java:511)
I/B4A     ( 2963): ~e:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
I/B4A     ( 2963): ~e:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
I/B4A     ( 2963): ~e:   at dalvik.system.NativeStart.main(Native Method)
I/B4A     ( 2963): ~e:java.lang.NullPointerException
 

bloxa69

Active Member
Licensed User
Longtime User
hey thedesolatesoul,
thanks for replying.

tried the small letters too, same result. I even decompiled classes.dex to check if the library's class is included in package - it's all there.

Also tried to add a intent helper to the library:

B4X:
   @ShortName("HWHelper")
   public static class HWHelper {
       public Intent GetMyIntent(BA pBA){
           Intent intent=new Intent(pBA.activity, HelloWord.class);
         return intent;
       }
   }

library compiles fine, i can add it to B4A, a see GetMyIntent method from B4A, B4A app compiles fine, when i decompile the package the library is there, but the problem is the same - the app cannot find the activity class.
(see the screenshot)

B4A code
B4X:
Dim hwh As HWHelper
hwh.GetMyIntent

Manifest entry (checked the actual manifest in the package):
B4X:
<activity android:name="com.helloword.HelloWord" />
 

Attachments

  • ssh.JPG
    ssh.JPG
    30.7 KB · Views: 209

bloxa69

Active Member
Licensed User
Longtime User
then I'll have to put it in a separate class, i mean a separate file and compile as a separate library?
 

thedesolatesoul

Expert
Licensed User
Longtime User
then I'll have to put it in a separate class, i mean a separate file and compile as a separate library?
At the moment it seems to be inside the HelloWord class.

Anyway, even if you put it in a different file, it can be compiled into the same library.
When you generate the javadoc/xml, make sure all the files are selected from source. Same for exporting the jar.
 

bloxa69

Active Member
Licensed User
Longtime User
I moved it outside, in a separate class. Now I don't get any errors but it still doesn't open the HelloWord activity,


HelloWord Activity

B4X:
package com.helloword;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@ActivityObject
@Author("HelloWord")
@Version(1.0f)

@ShortName("HelloWord")
public class HelloWord extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

        TextView text = new TextView(this);
        text.setText("Hello World of Android! - Greetings from Java Code Geeks");
        setContentView(text);
   }

}


Intent Helper:

B4X:
package com.helloword;


import android.content.Intent;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@ActivityObject
@ShortName("GLHelper")
public class GLHelper {
    public Intent GetMyIntent(BA pBA){
        Intent intent=new Intent(pBA.activity, HelloWord.class);
      return intent;
    }
}


B4A Code:

B4X:
Dim hwh As GLHelper
hwh.GetMyIntent


Android Manifest:

B4X:
AddApplicationText(<activity android:name="com.helloword.HelloWord" />)

Package screenshot attached. :BangHead:
 

Attachments

  • Capture.PNG
    Capture.PNG
    6 KB · Views: 227

bloxa69

Active Member
Licensed User
Longtime User
Added some log lines to both Helper class and Activity class.

GLHelper does get called from B4A and runs fine, so B4A does communicate with the library and executes at least that helper class, but Activity class never runs and no errors are generated.

Also, in the log it looks like instead of opening the new HelloWord Activity, the app opens the .main activity

B4X:
I/B4A     ( 1923): ~i:** Activity (main) Create, isFirst = true **
I/B4A     ( 1923): GLHelper do 1
I/B4A     ( 1923): GLHelper do 2
I/B4A     ( 1923): ~i:** Activity (main) Resume **
I/B4A     ( 1923): GLHelper do 1
I/B4A     ( 1923): GLHelper do 2

// here we go >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// opening .main again
I/ActivityManager(  275): Displayed com.helloworldapp/.main: +366ms
 
Last edited:

bloxa69

Active Member
Licensed User
Longtime User
Played with manifest and changed that line to anything, no difference, no errors:

B4X:
AddApplicationText(<activity android:name="com.helloword.HelloWord" />)


AddApplicationText(<activity android:name="com.nothing.Nothing" />)

and so on
 

bloxa69

Active Member
Licensed User
Longtime User
yes Erel,
that did it, that did the trick, thanks a lot,
thedesolatesoul - thanks for your help too, it much appreciated.

And B4A - thank you too, I know Java much better now thanks to Basic4Android. Sometimes it even seems that Java is easier, but I know it's just an illusion.:icon_clap:
 
Top