Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

This startActivityForResul() crashes when tested

 
Ranch Hand
Posts: 110
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, good evening.

I am exploring startActivityForResult. I am using a self-study book.

*  MainActivity starts ActivityC for a result. The method onClickGetData accomplishes this.
*  ActivityC returns the text typed into an EditText. The method onClickSendData does this.
*  MainActivity receives the data and displays it in a TextView.

However, when I tap "Send Data" in ActivityC the app crashes ("Unfortunately app has stopped working").

I am using Android Studio. In the original code from the self-study book everything is inside the onCreate method and uses inner classes to implement the button handlers.

My button handlers are outside the onCreate method so I've had to declare the TextView and EditView variables as class variables because they are used in multiple methods. I'm wondering whether these changes are causing the app to crash.

Any insights most welcome.




and class ActivityC is:

 
Marshal
Posts: 4550
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would start by looking at LogCat to see what it shows.
 
Mohammed Azeem
Ranch Hand
Posts: 110
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Morning Ron,

OK so, LogCat reveals the following pertinent lines:

01-23 03:19:29.331 7860-7860/com.example.hour2appversion2 E/AndroidRuntime: FATAL EXCEPTION: main

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
       at com.example.hour2appversion2.ActivityC.onClickSend(ActivityC.java:24)
       at java.lang.reflect.Method.invoke(Native Method)



It seems there is NullPointerException:

  *   Line 14 in the code listed in the post (but line 24 in the actual code) the the EditText object "e" is set to null

But how can this be, since I have declared e has an EditText object and assigned it in line 9 of Class ActivityC ?

It is 3.30 am in England so I will act on any replies tomorrow. Thanks.
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammed Azeem wrote:But how can this be, since I have declared e has an EditText object and assigned it in line 9 of Class ActivityC ?



Why not? Sure, you assigned it a value but the error message suggests you assigned a null value.

Which suggests that "findViewById(R.id.editText)" returns null. Why couldn't it do that?
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic