Search code examples
androidbroadcastreceivercounter

Counter through activities


I have a problem developing my app. I have a button on activity B which starts activity C. I have a textView on activity A. I would like to increase the activity A's textView by 1 with a counter every time the user touches my button in Activity B. Somebody told me i could use a BroadcastReceiver to send information to the activity A. I tried to use it, but when i open Activity A the app crashes. Can you help me?

sendBroadcast (activity B):

public static String BROADCAST_ACTION = "com.example.appquiz.firstimagelogo.COUNTER";

public void sendBroadcast(View v){
        Intent broadcast = new Intent();
        broadcast.setAction(BROADCAST_ACTION);
        sendBroadcast(broadcast);
    }

BroadcastReceiver (Activity A):

 //the whole code

package com.example.appquiz;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;

public class Levels extends Activity  {

     int counter;
    TextView txView ;

    public void sendMessage (View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_levels);
        // Show the Up button in the action bar.
        setupActionBar();
        ActionBar actionBar = getActionBar();
        actionBar.setIcon(R.drawable.list);
        actionBar.setTitle("Levels");
        counter=0;
        registerReceiver(mReceive , new IntentFilter(
                FirstImageLogo.BROADCAST_ACTION));
     txView = (TextView)findViewById(R.id.counter1);

}


    private BroadcastReceiver mReceive = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            counter++;
            txView.setText(counter + "");
        }

    };


    @Override
    public void onBackPressed() {
        Intent intent = new Intent (this, MainActivity.class);
        startActivity(intent);
        return;
    }

    @Override
    public void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(FirstImageLogo.BROADCAST_ACTION);
        registerReceiver(mReceive, filter);
        super.onResume();
    }

    @Override
    public void onPause() {
        unregisterReceiver(mReceive);
        super.onPause();
    }


    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.levels, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.action_settings:
            Intent intent = new Intent (this, Settings.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }


}

Manifest (activity A):

//codes before
    <intent-filter>
    <action android:name="com.example.appquiz.firstimagelogo.COUNTER"></action>
    </intent-filter>

Thank you :)

EDIT: When it crashes, this is the log cat:

01-29 15:26:16.190: D/OpenGLRenderer(22554): Enabling debug mode 0
01-29 15:26:17.210: D/dalvikvm(22554): GC_FOR_ALLOC freed 110K, 1% free 17784K/17932K, paused 10ms, total 10ms
01-29 15:26:18.420: D/AndroidRuntime(22554): Shutting down VM
01-29 15:26:18.420: W/dalvikvm(22554): threadid=1: thread exiting with uncaught exception (group=0x41c22ba8)
01-29 15:26:18.430: E/AndroidRuntime(22554): FATAL EXCEPTION: main
01-29 15:26:18.430: E/AndroidRuntime(22554): Process: com.example.appquiz, PID: 22554
01-29 15:26:18.430: E/AndroidRuntime(22554): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.appquiz/com.example.appquiz.Levels}: java.lang.NullPointerException
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.os.Looper.loop(Looper.java:136)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.ActivityThread.main(ActivityThread.java:5017)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at java.lang.reflect.Method.invoke(Method.java:515)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at dalvik.system.NativeStart.main(Native Method)
01-29 15:26:18.430: E/AndroidRuntime(22554): Caused by: java.lang.NullPointerException
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.Activity.findViewById(Activity.java:1884)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at com.example.appquiz.Levels.<init>(Levels.java:21)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at java.lang.Class.newInstanceImpl(Native Method)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at java.lang.Class.newInstance(Class.java:1208)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-29 15:26:18.430: E/AndroidRuntime(22554):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
01-29 15:26:18.430: E/AndroidRuntime(22554):    ... 11 more

Solution

  • Seems like you are not registering the receiver ...here is how you register it

    registerReceiver(mReceive , new IntentFilter(
                    ActivityA or whatever the name of the activity is.BROADCAST_ACTION));