-2

I made a button on my main activity which should open another activity. I declared the activity in AndroidManifest.xml and the function looks like this:

package com.example.cristian.befficient;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

public void addact(View v)
{
    startActivity(new Intent(MainActivity.this, NewAct.class));
}

The error is something about the actionbar, how do I fix it? Crash log:

--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.cristian.befficient, PID: 2299
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cristian.befficient/com.example.cristian.befficient.NewAct}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                  at com.example.cristian.befficient.NewAct.onCreate(NewAct.java:12)
                  at android.app.Activity.performCreate(Activity.java:6664)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Application terminated.

The activity I want to open:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView       
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cristian.befficient.NewAct">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:text="@string/large_text" />

And yes, it's declared in the manifest file

<activity
        android:name=".NewAct"
        android:label="@string/title_activity_new"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.cristian.befficient.MainActivity" />
    </activity>
  • Have you added your second activity to your manifest? We will need the crash log to be able to help you – Chol Nov 14 '16 at 15:44
  • Possible duplicate of [Android Studio marks R in red with error message "cannot resolve symbol R", but build succeeds](http://stackoverflow.com/questions/17421104/android-studio-marks-r-in-red-with-error-message-cannot-resolve-symbol-r-but) – ItamarG3 Nov 14 '16 at 15:45
  • Your action bar seems to be null! Can you paste your second activity – Chol Nov 14 '16 at 15:46
  • `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference`. – greenapps Nov 14 '16 at 15:49
  • You must be using a theme without action bar which is why getSupportActionBar() returns null. Change the theme or dont use action bar! – Prasad Pawar Nov 14 '16 at 17:54

5 Answers5

0

Go to build and press clean poject. (Build -> Clean Project ) This will help you to resolve Cannot resove symbol 'R' error

Zayid Mohammed
  • 2,275
  • 2
  • 10
  • 17
  • I still have an error, Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at com.example.cristian.befficient.NewAct.onCreate(NewAct.java:12) – Cristian Maranca Nov 14 '16 at 15:59
0

use

import com.example.cristian.befficient.R;

given that com.example.cristian.befficient is the package of the app.

Edo user1419293
  • 171
  • 2
  • 9
  • i solved the error with the R, i have an error with something called an actionbar – Cristian Maranca Nov 14 '16 at 16:02
  • Clearly the error is Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at com.example.cristian.befficient.NewAct.onCreate(NewAct.java:12), That means that you are calling the method setDisplayHomeAsUpEnabled on a null reference. You should share NewAct.java – Edo user1419293 Nov 14 '16 at 16:37
0

use supportActionBar instead of ActionBar in your NewAct line 12

Anas Altair
  • 170
  • 2
  • 9
0

Action bar error

  • used getSupportActionBar() instead of getActionBar()
  • public class NewAct extends AppCompatActivity
Danh
  • 5,916
  • 7
  • 30
  • 45
0

Your action bar may be null for whatever reason. Check out your styling for the main activity and ensure that the action bar is enabled.

Check out your - values/styles.xml and ensure that it has an action bar.

<style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

Check out this link for more info https://developer.android.com/design/patterns/actionbar.html

Andrew Nguyen
  • 106
  • 11