-2

I dont know why my app crashes . When i remove the CODE dynamic broadcastReceicer , my app runs perfect . Please help i am a beginner

MainActivity.java

//Button b1 = (Button)findViewById(R.id.button1);

Button b2 = (Button)findViewById(R.id.button2);

TextView t1 = (TextView)findViewById(R.id.textView1);
private static final String Dynamic_Regis_Intent = "com.example.week_5_broadcastreceiver.CUSTOM_DYNAMIC";



//---dynamic regis----

private final IntentFilter intentfilter = new IntentFilter(Dynamic_Regis_Intent);
private BroadcastReceiver rec =new receiver_dynamically();
private LocalBroadcastManager mbroadcastmgr;

//----dynamic regis ------


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1 = (Button)findViewById(R.id.button1);

    b1.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            Intent a= new Intent("Static Intent");
            a.setAction("com.example.week_5_broadcastreceiver.CUSTOM_STATIC");
            sendBroadcast(a);
        }
    });

    //dynamic

    mbroadcastmgr = LocalBroadcastManager.getInstance(getApplicationContext());
    mbroadcastmgr.registerReceiver(rec, intentfilter);

    b2.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            mbroadcastmgr.sendBroadcast(new Intent(Dynamic_Regis_Intent));

        }


    });
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onDestroy(){

    mbroadcastmgr.unregisterReceiver(rec);
}

}

receiver.java

public class receiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(arg0, "Got The Intent", Toast.LENGTH_SHORT).show();
    }
}

receiver_dynamically.java

public class receiver_dynamically extends BroadcastReceiver
{
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(arg0, "Got The Dynamic Intent", Toast.LENGTH_SHORT).show();
    }
}
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
Rahul Lohra
  • 15
  • 1
  • 5
  • 1
    What is the crash exactly? On which line? Please post logcat dump. – Anton Savin Nov 29 '14 at 22:05
  • 1
    possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Anton Savin Nov 29 '14 at 22:06
  • Thank you Anton for taking a look into my question , my problem has been solved, i just did a silly mistake , i was instantiating the BUTTON and textbox before oncreate() – Rahul Lohra Nov 30 '14 at 18:11

1 Answers1

0

I was making object of BUTTON and TEXTVIEW , before onCreate(), thats why my app was just crashing during startup and i also remove the LOCALBROADCAST MANAGER.

Rahul Lohra
  • 15
  • 1
  • 5