-1

I'm sorry for asking such a basic question, but I haven't found anything that could help me with this issue. I'm trying to do something as simple as starting a new activity through a button click, but my application crashes whenever I click that button. Here is my code:

Button aboutButton = (Button)findViewById(R.id.aboutButton);
    aboutButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Intent intent = new Intent(MainActivity.this, About.class);
            startActivity(intent);
        }
    });

This code is inside a function, which is in turn inside the onCreate function. I've already tried putting this code straight into the onCreate function, but I got the same result. Could you please tell me what I have done wrong?

PS.: My target activity is already declared at the manifest.

Error log :

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ukdev.TestApp/com.ukdev.TestApp.About}: java.lang.NullPointerException
Sina R.
  • 1,781
  • 2
  • 19
  • 37
92AlanC
  • 1,327
  • 2
  • 14
  • 33
  • @ρяσѕρєяK java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ukdev.TestApp/com.ukdev.TestApp.About}: java.lang.NullPointerException – 92AlanC Feb 07 '16 at 03:12
  • 1
    Ok,Show About class code also – ρяσѕρєя K Feb 07 '16 at 03:13
  • which line throws the NPE? – rothloup Feb 07 '16 at 03:15
  • @rothloup: startActivity(intent) I guess, the problem is in About, and the question itself of course... – Gavriel Feb 07 '16 at 03:16
  • 1
    I debugged my code and I saw that my IDE has already created another Intent called intent, which has not been previously assigned, so when the application goes to the `startActivity(intent);` it pointed to the unassigned variable. Problem solved. Thanks @ρяσѕρєяK for prompting me to check the log and all others for the effort – 92AlanC Feb 07 '16 at 03:20

2 Answers2

0

Your About class has to extend Activity or AppCompatActivity, does yours?

public class MainActivity extends Activity { ...

if it does then we would just need the rest of your code.

iBobb
  • 1,140
  • 1
  • 14
  • 35
0

That NullPointerException came from an unassigned intent variable, which was called by the startActivity(intent); function.

92AlanC
  • 1,327
  • 2
  • 14
  • 33