0

Do you know why does it crash?

I will show you my testing java file. Android.mk, c++ file and Application.mk are not important.

This is java file what works only when there isnt BUTTON part. When I run it as you can see with BUTTON part it crashes. Do you know why? What affect it?

package com.example.hellojnicpp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Hellojnicpp extends Activity {
 public native String stringFromJNI();
 static {
 System.loadLibrary("algo-rithm");
 }
 Button button1;
 TextView tv1;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 TextView tv = new TextView(this);
 tv.setText(stringFromJNI());
 setContentView(tv); 
//////////////////////////////BUTTON////////////////////////////////////// 
 button1 = (Button)findViewById(R.id.button1);
 button1.setOnClickListener(new OnClickListener(){
 public void onClick(View view){ 
 tv1 = (TextView)findViewById(R.id.textView1);
 tv1.setText("Hello");
 }
 });
///////////////////////////////BUTTON/////////////////////////////////// 
 }
}

This is LogCat output:

05-01 18:34:34.030: D/dalvikvm(18786): Trying to load lib /data/data/com.example.hellojnicpp/lib/libalgo-rithm.so 0x409e3998
05-01 18:34:34.030: D/dalvikvm(18786): Added shared lib /data/data/com.example.hellojnicpp/lib/libalgo-rithm.so 0x409e3998
05-01 18:34:34.030: D/dalvikvm(18786): No JNI_OnLoad found in /data/data/com.example.hellojnicpp/lib/libalgo-rithm.so 0x409e3998, skipping init
05-01 18:34:34.040: D/AndroidRuntime(18786): Shutting down VM
05-01 18:34:34.040: W/dalvikvm(18786): threadid=1: thread exiting with uncaught exception (group=0x40185760)
05-01 18:34:34.050: E/AndroidRuntime(18786): FATAL EXCEPTION: main
05-01 18:34:34.050: E/AndroidRuntime(18786): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellojnicpp/com.example.hellojnicpp.Hellojnicpp}: java.lang.NullPointerException
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.ActivityThread.access500ドル(ActivityThread.java:122)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.os.Looper.loop(Looper.java:132)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.ActivityThread.main(ActivityThread.java:4123)
05-01 18:34:34.050: E/AndroidRuntime(18786): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 18:34:34.050: E/AndroidRuntime(18786): at java.lang.reflect.Method.invoke(Method.java:491)
05-01 18:34:34.050: E/AndroidRuntime(18786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
05-01 18:34:34.050: E/AndroidRuntime(18786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
05-01 18:34:34.050: E/AndroidRuntime(18786): at dalvik.system.NativeStart.main(Native Method)
05-01 18:34:34.050: E/AndroidRuntime(18786): Caused by: java.lang.NullPointerException
05-01 18:34:34.050: E/AndroidRuntime(18786): at com.example.hellojnicpp.Hellojnicpp.onCreate(Hellojnicpp.java:29)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.Activity.performCreate(Activity.java:4397)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-01 18:34:34.050: E/AndroidRuntime(18786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
05-01 18:34:34.050: E/AndroidRuntime(18786): ... 11 more
05-01 18:40:55.340: D/dalvikvm(19183): Trying to load lib /data/data/com.example.hellojnicpp/lib/libalgo-rithm.so 0x409e4fa0
05-01 18:40:55.340: D/dalvikvm(19183): Added shared lib /data/data/com.example.hellojnicpp/lib/libalgo-rithm.so 0x409e4fa0
05-01 18:40:55.340: D/dalvikvm(19183): No JNI_OnLoad found in /data/data/com.example.hellojnicpp/lib/libalgo-rithm.so 0x409e4fa0, skipping init

I am very curious why it is happening...

Thank you in advance.

Chris Stratton
40.5k6 gold badges89 silver badges120 bronze badges
asked May 1, 2015 at 16:48
2
  • The log complains about Hellojnicpp.java line 29. Which line is that? Commented May 1, 2015 at 17:30
  • this one button1.setOnClickListener(new OnClickListener(){ Commented May 1, 2015 at 17:36

2 Answers 2

2

This is java file what works only when there isnt BUTTON part.

 TextView tv = new TextView(this);
 tv.setText(stringFromJNI());
 setContentView(tv); 
 button1 = (Button)findViewById(R.id.button1);

Your set your ContentView to be just one solitary TextView, rather than an XML layout or a ViewGroup of any sort.

Then you try to find something called R.id.button1 inside the ContentView, which is just your TextView which cannot contain a Button. That means your button1 variable is null, so of course this:

button1.setOnClickListener(new OnClickListener()...

Triggers an exception

answered May 1, 2015 at 17:53
Sign up to request clarification or add additional context in comments.

Comments

2

Unfortunately, button1 is null. The method findViewById(R.id.button1) looks for this button in current content view, which is a dynamically generated TextView, not some layout loaded from XML resources. It is quite natural that this view does not have a child with android.id="@+id/button1"

answered May 1, 2015 at 17:51

1 Comment

Oh, you are completely right... beginner's mistake. Thank you very much gentlemens!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.