-2

enter image description hereI want when I click the button. showing Connectingreceiver.class

but NullpointerException. I think I not well use context. advice for me thanks android senior developer.

private OnClickListener mConnectOnClick = new OnClickListener() {
    Context context= ConfiguredNetworkContent.this;

   @Override
    public void onClick(View v) {


       Intent intent = new Intent(context,Connectingreceiver.class);
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent);





 public class Connectingreceiver extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.connecting_dialog);


}
}
조현욱
  • 31
  • 8

5 Answers5

1

where this context? Context context;

fix Context context = getApplicationContext;

PeDuCKA
  • 553
  • 3
  • 13
1

Check this :

private View.OnClickListener mConnectOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
    Intent intent = new Intent(v.getContext(), Connectingreceiver.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    v.getContext().startActivity(intent);
}

Update :

Straight forward explanation is you need a Context to start a Activity. And in Onclick() there is a passing View in which Context already existed. So, I only used it to start activity.

Sayem
  • 4,891
  • 3
  • 28
  • 43
1

Try this

// Add this line after Main class
Button yourButton;

In below code don't forget to edit "yourButtonIDfromXML"

// Add below code in OnCreate method after setContentView
Button yourButton = (Button) findViewById(R.id.yourButtonIDfromXML);
yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ConfiguredNetworkContent.this, Connectingreceiver.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        });
Uday
  • 275
  • 1
  • 4
  • 15
0

I think I not well use context.

You have to use it if needed, But as per your codes there is no initialization of context.

First initialize your context either with ApplicationContext or ActivityContext like here

Context context = YourActivity.this;

then you can use startActivity(intent);. You don't need to write context.startActivity(intent);. only startActivity will be enough at all.

UPDATE :

Do not pass any context their just simply do

customstartActivity(); 

And inside customstartActivity() method

public static void customstartActivity (){
 //  Intent intent = new Intent(yourActivity.this, Connectingreceiver.class);
 //  Intent intent = new Intent(ContextWrapper.getBaseContext(), Connectingreceiver.class);
     Intent intent = new Intent(getApplicationContext(), Connectingreceiver.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • I try `private OnClickListener mConnectOnClick = new OnClickListener(){ Context context = myclass.this;` – 조현욱 Mar 28 '16 at 06:35
  • and `@Override public void onClick(View v){ Intent intent = new Intent(context, ,Connectingreceiver.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent) ;' but 'Intent intent = new Intent(context,Connectingreceiver.class);' this part NPE – 조현욱 Mar 28 '16 at 06:37
  • And will this work if you put `getApplicationContext()` in the place of `context`. – Shree Krishna Mar 28 '16 at 06:44
  • I try in onClick `Intent intent = new Intent(getApplicationContext(),Connectingreceiver.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);` but NPE error.. sorry @Shree Krishna – 조현욱 Mar 28 '16 at 06:48
  • Is `Connectingreceiver` a seperate Activity (class) or same as you've shown their ? – Shree Krishna Mar 28 '16 at 06:50
  • `Connectingreceiver` only `public class Connectingreceiver extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.connecting_dialog); } }` – 조현욱 Mar 28 '16 at 06:52
  • my class is extends BaseContent. BaseContent is abstract class extends Activity – 조현욱 Mar 28 '16 at 06:52
  • See my update. And see the commented intent too.. Use whichever you prefer. Don't be so complex. – Shree Krishna Mar 28 '16 at 06:55
0

The "context" - is null. You need to do null check s to avoid Null Pointer Crash. It is the easiest exception to resolve.

If your are doing OnClick event in

  1. Activity:- Use these lines.

    public static void startReceiver() {

        Intent intent = new Intent(YourActivityName.this, Connectingreceiver.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    
  2. Fragment :-
    Use these lines

    private OnClickListener mConnectOnClick = new OnClickListener() {

    Context context;
    @Override
    public void onClick(View v) {
        if(getActivity() != null) {
            startActivity(getActivity());
        }
    }
    

    And use same the startActivity() method

Also, rename your startActivity method. It is an inbuilt android method. Prefer using camel cases for your class name. Connectingreceiver --> ConnectingReceiver.

Yashika
  • 173
  • 1
  • 6