-3

I've just started learning android and i am trying to open an activity upon clicking on a textView. As far as I can see, there is not problem with my code, and I debugged it but couldn't find any calling to null. This is the exception:

Process: com.example.yuval.happybirthday, PID: 8057
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                               at android.content.ContextWrapper.getPackageName(ContextWrapper.java:131)
                                                                               at android.content.ComponentName.<init>(ComponentName.java:77)
                                                                               at android.content.Intent.<init>(Intent.java:4214)
                                                                               at com.example.yuval.happybirthday.NumbersClickListener.onClick(NumbersClickListener.java:19)
                                                                               at android.view.View.performClick(View.java:4811)
                                                                               at android.view.View$PerformClick.run(View.java:20136)
                                                                               at android.os.Handler.handleCallback(Handler.java:815)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                               at android.os.Looper.loop(Looper.java:194)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5549)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

This is the Main activity:

package com.example.yuval.happybirthday;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    NumbersClickListener pls = new NumbersClickListener();
    ((TextView) findViewById(R.id.numbers)).setOnClickListener(pls);
}
}

This is the NumberClickListener class:

public class NumbersClickListener extends Activity implements View.OnClickListener {
@Override
public void onClick(View view){
    Intent i = new Intent(view.getContext(),NumbersActivity.class);
    startActivity(i);
}
}

And this is the numbersActivity class:

public class NumbersActivity extends AppCompatActivity {

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

I tried debuggin it severl times and just couldn't find where the problem is.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yuval Buium
  • 109
  • 1
  • 1
  • 4

2 Answers2

0

Replace:

public class NumbersClickListener extends Activity implements View.OnClickListener

with:

public class NumbersClickListener implements View.OnClickListener

as NumbersClickListener is not being used as an Activity.

This, in turn, will require you change startActivity(i) to view.getContext().startActivity(i). That probably works, though I am not completely certain.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • tnx! it worked. May you explain me whhy extending an activity didn't work? and how did you know that view.getcontext would have startActivity in it? – Yuval Buium May 16 '17 at 05:16
0

You are creating ClickListener , so no need to extend Activity. Try below code :-

public class NumbersClickListener implements View.OnClickListener {

@Override
public void onClick(View view){
Intent i = new Intent(view.getContext(),NumbersActivity.class);
view.getContext().startActivity(i);
}

}

Dany
  • 71
  • 1
  • 5