Exception while tried to set a Text on a TextView after creating an Object from Activity
<p>I got stuck yesterday on programming an android app.
First of all, i solved my problem by sending an broadcast intent and react on this. But please, could you help me to unterstand the issue:</p>

<p>I try to update an TextView:</p>

<pre><code>public void setProgressBarState(String daten) {
    try {

    txt.setText(daten);
    }
    catch (Exception e) {
    Log.d(getPackageName(), daten, e);  
    }
    //progressBarHandler.sendMessage(progressBarHandler.obtainMessage(0, 0, 0,daten));
}
</code></pre>

<p>(String daten) - this String coming from a service, wich listen on a BluetoothSocket. A MessageHandler send the received data to setProgressBarState method:</p>

<pre><code>public void onCreate() {
    dataHandler = new Handler() {
        public void handleMessage(Message msg) {
            String daten=(String) msg.obj;
            //Toast.makeText(BluetoothService.this, daten, Toast.LENGTH_SHORT).show();
            RemoteControl remote = new RemoteControl();
            remote.setProgressBarState(daten);
            Intent intent = new Intent("DataAvialable");
            intent.putExtra("DATA", daten);
            LocalBroadcastManager.getInstance(ctx).sendBroadcastSync(intent);



        }
    };



}
</code></pre>

<p>(You can see, I solved my Problem by using Broadcast...) 
When debugging, I see a NullPointerException when "txt.setText(Daten)" is processed. </p>

<p>I tried to do</p>

<pre><code>TextView txt = (TextView) findViewById (R.id.TextView2);
</code></pre>

<p>before, but then I get an InvocationTargetException. </p>

<p>I think it is a really easy thing, but sorry, I was not able to solve this (only with my broadcast)... Please can anyone tell me why it is not possible to set the text in the TxextView...</p>

<p>Sorry for my english, I will answer any question, if something is not clear. </p>

<p>Thank you in advance!</p>

<p>Edit: Hope this helps....</p>

<pre><code>&gt; 04-26 13:23:34.279: E/AndroidRuntime(9464): FATAL EXCEPTION: main
04-26 13:23:34.279: E/AndroidRuntime(9464): java.lang.NullPointerException
04-26 13:23:34.279: E/AndroidRuntime(9464):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:123)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at com.example.bluecomm.RemoteControl.setProgressBarState(RemoteControl.java:117)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at com.example.bluecomm.BluetoothService$1.handleMessage(BluetoothService.java:67)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at android.os.Looper.loop(Looper.java:130)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at java.lang.reflect.Method.invokeNative(Native Method)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at java.lang.reflect.Method.invoke(Method.java:507)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
04-26 13:23:34.279: E/AndroidRuntime(9464):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Sorry, it seems iam not really able to framt right...
here is XML</p>

<pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
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=".BlueCommMain" &gt;

&lt;Button
    android:id="@+id/vol_Mute"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/vol_Up"
    android:layout_marginLeft="34dp"
    android:text="@string/Vol_Mute_Button" /&gt;

&lt;Button
    android:id="@+id/vol_Up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="53dp"
    android:layout_marginTop="69dp"
    android:text="@string/Vol_Up_Button" /&gt;

&lt;Button
    android:id="@+id/vol_Down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/vol_Up"
    android:layout_below="@+id/vol_Mute"
    android:text="@string/Vol_Down_Button" /&gt;

&lt;TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/vol_Down"
    android:layout_below="@+id/vol_Down"
    android:layout_marginTop="46dp"
    android:text="Aktuelle Lautstärke:"
    android:textAppearance="?android:attr/textAppearanceLarge" /&gt;

&lt;ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="26dp" /&gt;

&lt;TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/progressBar1"
    android:layout_below="@+id/progressBar1"
    android:layout_marginLeft="42dp"
    android:layout_marginTop="20dp"
    android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
</code></pre>

<p></p>

<p>an this is my Activity</p>

<pre><code>    ackage com.example.bluecomm;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class RemoteControl extends Activity {
String mac = null;
Context ctx;
ProgressBar progress;
Handler progressBarHandler;
TextView txt;
BroadcastReceiver mMessageReceiver;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(getPackageName(), "onCreate");
        setContentView(R.layout.volume_remote);
        txt = (TextView) findViewById(R.id.textView2);
        Intent intent = getIntent();
        Bundle daten = intent.getExtras();
        mac = daten.getString("MAC");
        //progress=new ProgressBar(RemoteControl.this);
        progress=(ProgressBar)findViewById(R.id.progressBar1);
        final Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);






        Button btn_Vol_Up = (Button) findViewById (R.id.vol_Up);
        btn_Vol_Up.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick (View v) {
                Intent service = new Intent(v.getContext(), BluetoothService.class);
                service.putExtra("MAC", mac);
                service.putExtra("MESSAGE", "VOL_UP");

                vib.vibrate(30);
                startService(service);

            }});

        Button btn_Vol_Down = (Button) findViewById (R.id.vol_Down);
        btn_Vol_Down.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick (View v) {
                Intent service = new Intent(v.getContext(), BluetoothService.class);
                service.putExtra("MAC", mac);
                service.putExtra("MESSAGE", "VOL_DOWN");
                vib.vibrate(50);
                startService(service);
            }});

        Button btn_Mute = (Button) findViewById (R.id.vol_Mute);
        btn_Mute.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick (View v) {
                Intent service = new Intent(v.getContext(), BluetoothService.class);
                service.putExtra("MAC", mac);
                service.putExtra("MESSAGE", "MUTE");
                vib.vibrate(100);
                startService(service);
            }});




        mMessageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
             Log.i(getPackageName(), "Got Intent");
             String volume=intent.getStringExtra("DATA");
             Double volume_dbl = Double.valueOf(volume);
             volume_dbl=volume_dbl*100;
             int volume_int=(int)Math.round(volume_dbl);
             txt.setText(String.valueOf(volume_int));
             progress.setProgress(volume_int);
            }
        };


        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                new IntentFilter("DataAvialable"));
    }






    public void setProgressBarState(String daten) {
        try {

        txt.setText(daten);
        }
        catch (Exception e) {
        Log.d(getPackageName(), daten, e);  
        }
            }

    @Override
    public void onResume(){
       super.onResume();
       Log.i(getPackageName(), "onResume");

    }






    @Override
    public void onPause(){
        super.onPause();

        LocalBroadcastManager.getInstance(ctx).unregisterReceiver(mMessageReceiver);
        Log.i(getPackageName(), "onPause");
        //stopService(service);

    }

    }
</code></pre>
