This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

This startActivityForResul() crashes when tested

 
Ranch Hand
Posts: 75
3
  • Mark post as helpful
  • send pies
  • 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:

 
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by looking at LogCat to see what it shows.
 
Mohammed Azeem
Ranch Hand
Posts: 75
3
  • Mark post as helpful
  • send pies
  • 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: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!