2

I have a widget and an activity, when I start the activity, I have to read text from the TextView of the widget and show it in the TextWidget of the activity.

My relevant code:

 String frase=""; 
 TextView text_frase = (TextView) findViewById(R.id.widget_textview_frase);
 if (text_frase != null){
    frase = (String) text_frase.getText();
 }
 Log.v(LOG_CLASS_NAME, "frase: "+frase);

debugging it - I have text_frase as null (I think I can't reference to a view object from another view.) Anyway, how could I do this?

Dimitar
  • 4,402
  • 4
  • 31
  • 47
Gabriele Pia
  • 53
  • 2
  • 7
  • Are you calling activity2 from activity 1?, if so you need to pass it in bundle – kosa Mar 03 '12 at 00:25
  • Thanks... but what you call activity1 is a widget... i prepare a pending intent... so i can't. And... i need to get content on activity start. – Gabriele Pia Mar 03 '12 at 00:33

2 Answers2

4

No, you can't. I suggest one of this ways:

  1. Adding your text to a Bundle and fetching it in your activity.

in your widget:

Intent intent = new Intent(context, YourActivity.class);
Bundle b= new Bundle();
b.putString("Text", yourtext);
intent.putExtras(b);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);

in your activity:

Intent intent=getIntent();
Bundle b= intent.getExtras();
String text= b.getString("Text", "");
  1. Using SharedPreferences for saving your text and loading it in the activity.

In your situation, I recommend method 1.

Mbt925
  • 1,317
  • 1
  • 16
  • 31
  • great... i think sharedPreferences is the best way... the firstone is not good becose i have a service that update the contento of TextView. – Gabriele Pia Mar 11 '12 at 10:48
0

you might want to get the actual String by using the .toString() method on the TextView.

String frase; 

TextView text_frase = (TextView) findViewById(R.id.widget_textview_frase);
if (text_frase.getText().toString() != null){
   frase = (String) text_frase.getText().toString();
   Log.v(LOG_CLASS_NAME, "frase: "+frase);
}
  • Thanks... but the TextView called **text_frase** is null... (as i wrote in the answer...) i think i can't find views of a widget using **findViewById** from the Activity. – Gabriele Pia Mar 03 '12 at 07:32