1

Get the user input:

Intent intentTrashcanLocation = new Intent(this, TrashcanLocationActivity.class);           
EditText editWidth = (EditText)findViewById(R.id.dimvalWidth);
EditText editLength = (EditText)findViewById(R.id.dimvalLength);
String width = editWidth.getText().toString();
String length = editLength.getText().toString();
intentTrashcanLocation.putExtra("widthValue", width);
intentTrashcanLocation.putExtra("lengthValue", length);
startActivity(intentTrashcanLocation);

Receive and display:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trashcan_location);        
Intent parentintent = getIntent();
Bundle dimval = parentintent.getExtras();
String width = (String)dimval.get("widthValue");    
String length = (String)dimval.get("lengthValue");
TextView widthtext = (TextView)findViewById(R.id.widthView);
TextView lengthtext = (TextView)findViewById(R.id.lengthView);
widthtext.setText(width);
lengthtext.setText(length);

When I run the program and give the text input and proceed to next activity, the program crashes. Getting errors:

03-29 12:33:57.418: E/AndroidRuntime(28049): FATAL EXCEPTION: main
03-29 12:33:57.418: E/AndroidRuntime(28049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trash2/com.example.trash2.TrashcanLocationActivity}: java.lang.NullPointerException
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.os.Looper.loop(Looper.java:137)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.ActivityThread.main(ActivityThread.java:5103)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at java.lang.reflect.Method.invokeNative(Native Method)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at java.lang.reflect.Method.invoke(Method.java:525)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at dalvik.system.NativeStart.main(Native Method)
03-29 12:33:57.418: E/AndroidRuntime(28049): Caused by: java.lang.NullPointerException
03-29 12:33:57.418: E/AndroidRuntime(28049):    at com.example.trash2.TrashcanLocationActivity.onCreate(TrashcanLocationActivity.java:36)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.Activity.performCreate(Activity.java:5133)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 12:33:57.418: E/AndroidRuntime(28049):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-29 12:33:57.418: E/AndroidRuntime(28049):    ... 11 more

I am pretty new at this and any help would be really appreciated. Thanks!

Edit: This is activity_trashcan_location:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.trash2.TrashcanLocationActivity"
tools:ignore="MergeRootFrame" />

and fragment_trashcan_location:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/interface_bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.trash2.TrashcanLocationActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="11dp"
    android:text="Bluetooth Trashcan"
    android:textAppearance="@style/AppTheme"
    android:textSize="60dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="10dp"
    android:text="- Settings: Trashcan Location?"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="40dp" />

 <TextView
    android:id="@+id/widthView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />    

 <TextView
    android:id="@+id/lengthView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />      

<Button
    android:id="@+id/buttonNext"
    android:layout_width="140dp"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="19dp"
    android:layout_marginRight="20dp"
    android:text="Finish"
    android:textSize="30dp"
    android:onClick="doMain" />

stedy
  • 253
  • 3
  • 11

5 Answers5

2

Your activity_trashcan_location layout does not contain the views widthView and lengthView. Instead they are in the fragment layout, and it won't be a part of your activity's view hierarchy in activity onCreate().

To fix it, either

  • move the code that touches the views to the fragment, e.g. its onCreateView(), or

  • move the views that need to be touched to the activity layout.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Oh so to clarify... activity_xxx is the main page, and fragment_xxx is just a "fragment" of that page? So in my case, 1 fragment is filling up this main page, but all the systematic stuff should be done on the activity_xxx ? – stedy Mar 29 '14 at 18:03
  • From the activity view hierarchy point of view (where activity `findViewById()` works), the fragment will be part of that hierarchy eventually but not yet in activity `onCreate()`. – laalto Mar 29 '14 at 18:05
  • Okay, this helped a lot! I probably need to fix a lot before I can test this.. Thanks! – stedy Mar 29 '14 at 18:08
  • So i'm trying to do the second option that you provided. Does the syntax change between the fragment and activity? Just cutting the relevant Textview code from fragment and pasting in the activity doesn't seem to do the job.. – stedy Mar 29 '14 at 18:29
  • In `onCreateView()` you usually inflate the fragment view. Call `findViewById()` on the view you just inflated. – laalto Mar 29 '14 at 18:31
  • So the first option is more recommended? When I tried it it gives me an error saying that I cannot make static reference to the non-static method findViewByID :( – stedy Mar 29 '14 at 18:38
  • Oh.. yes the second option you asked about.. Copy the layout elements to the other layout file. That should do it. But the first option is more recommended (though it has slightly steeper learning curve). You inflate the `view` and then call `view.findViewById(...)`. – laalto Mar 29 '14 at 18:42
2

All you have is a FrameLayoutin activity_trashcan_location.xml.

But you initialize TextView's in the Activtiy which leads to NullPointerException

The TextView's belong to the FragmentLayout. So you need to initialize textview in Fragment.

Get the value in Activtiy via intent and then communicate to Activity

Edit:

In Activity

String length = (String)dimval.get("lengthValue");
MyFragment newFragment = new MyFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fcontainer, newFragment);
transaction.addToBackStack(null); // If you want to add it to backstack
transaction.commit();

Then use the code by prosper k

Send data from activity to fragment in android

Then in Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_trashcan_location, container, false);

TextView widthtext = (TextView)rootView.findViewById(R.id.widthView);
TextView lengthtext = (TextView)rootView.findViewById(R.id.lengthView);
// set text to textView here
return rootView;
}
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Yes.. I seem to have misunderstood some stuff from the root. Repeated from the bottom: Oh so to clarify... activity_xxx is the main page, and fragment_xxx is just a "fragment" of that page? So in my case, 1 fragment is filling up this main page, but all the systematic stuff should be done on the activity_xxx ? – stedy Mar 29 '14 at 18:06
  • @Diletante no you are wrong. Fragment is attached to the Activity. However the views belong to the Fragment. So the intialization of view must be in Fragment – Raghunandan Mar 29 '14 at 18:07
  • Oh.. I think I am partially understanding it but not completely. I think I test this stuff out to understand it. Thank you so much – stedy Mar 29 '14 at 18:11
  • @Diletante `findViewById` looks for a view with the id in the current view hierarchy. So if your activity layout set to the activity does not contain those view you end up in NPE> – Raghunandan Mar 29 '14 at 18:13
  • I am so sorry both you and laalto helped me a lot but I had to choose the one that was posted just a bit earlier... Thanks again! – stedy Mar 29 '14 at 18:13
  • This is awesome! Thanks let me try this out too – stedy Mar 29 '14 at 18:31
1

Send them like that:

Intent intentTrashcanLocation = new Intent(this, TrashcanLocationActivity.class);           
Bundle bundle = new Bundle();
bundle.putString("widthValue", width);
bundle.putString("lengthValue", length);
intent.putExtras(bundle);

Get them like that:

Bundle bundle= this.getIntent().getExtras();
String width = (String)bundle.getString("widthValue");    
String length = (String)bundle.getString("lengthValue");
mertsimsek
  • 266
  • 2
  • 10
0

In your "get user code" code:

  1. Make sure you have a call to setContentView(R.layout.activity_trashcan_location) before the findViewById() calls. Otherwise, the findViewById() calls won't find anything, and return null.
  2. Make sure your activity_trashcan_location layout actually has components with the dimvalWidth and dimvalLength IDs. If those IDs are not assigned to a component in the layout file, the corresponding findViewById() call will also return null.
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • 1. I call setContentView in the OnCreate method, and call findViewById in the method that is called when the button is pressed on this activity.. would this not work? – stedy Mar 29 '14 at 18:01
  • Yes. Your question did not provide enough code to let me know this. In the future, you should provide much more code. For example, if your exception stack trace says the exception was thrown at line n, you should provide enough information so that we can identify line n. – cybersam Mar 29 '14 at 18:31
  • I will keep this in mind, sorry about the confusion! – stedy Mar 29 '14 at 18:33
-2

you need a handler to set a textview.... code to follow

declare vars before oncreate

Handler handler;
    private int i;

in oncreate:

i = 0;
        handler = new Handler();
        handler.post(mUpdate);

and the actuall method

Runnable mUpdate = new Runnable() {
        public void run() {


            yourtextview.setText("text you want to see");

            i++;

            handler.postDelayed(this, 1000);
        }
Technivorous
  • 1,682
  • 2
  • 16
  • 22
  • Handler? Why is that required??. Its not required at all – Raghunandan Mar 29 '14 at 17:48
  • you cant update the ui without a handler or asynctask. handler is easier( for me) – Technivorous Mar 29 '14 at 17:52
  • You cannot update ui from a non ui thread. Here in op's case that is not the case. Your totally wrong and misunderstood the use of Handler. I suggest you read the docs about ui thread and handlers – Raghunandan Mar 29 '14 at 17:53
  • umm yes you can, i do it all the time. try the code before you tell me im wrong. lol. it may not be how you do it, but it is a sloution and will fix the problem. and ive read those docs, over and over..... – Technivorous Mar 29 '14 at 17:55
  • Why Handler is not required. 1. read http://developer.android.com/reference/android/os/Handler.html 2. read Threads @ http://developer.android.com/guide/components/processes-and-threads.html – Raghunandan Mar 29 '14 at 17:59
  • After reading those 2 links come back and then say if i am wrong or not – Raghunandan Mar 29 '14 at 18:00
  • read em several times. part of the beauty of code is bieng able to think outside the box. i have a handler that updates my ui with about..... 100 textviews, and a few imageviews, it also set my layouts visiblity for about 15 layouts. just because its not how you use it dosnt make it wrong, it is stable and quick. – Technivorous Mar 29 '14 at 18:01
  • (2) to enqueue an action to be performed on a different thread than your own. (such as the uithread) – Technivorous Mar 29 '14 at 18:01
  • You don't get the point there is no need for a handler. well i am not here to argue. good luck – Raghunandan Mar 29 '14 at 18:02
  • 03-29 12:33:57.418: E/AndroidRuntime(28049): at android.os.Handler.dispatchMessage(Handler.java:99) – Technivorous Mar 29 '14 at 18:03