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.