-2

I am developing this app for my project but every time i run it I get a message that says "Unforunately, ConcertProject has stopped". I don't know whats wrong with it

this is the error i get

03-20 18:30:43.845 6814-6814/? I/art: Not late-enabling -Xcheck:jni (already on)
03-20 18:30:44.070 6814-6814/net.androidbootcamp.concertproject W/System: ClassLoader referenced unknown path: /data/app/net.androidbootcamp.concertproject-1/lib/x86
03-20 18:30:44.784 6814-6822/net.androidbootcamp.concertproject W/art: Suspending all threads took: 24.445ms
03-20 18:30:45.099 6814-6822/net.androidbootcamp.concertproject W/art: Suspending all threads took: 13.043ms
03-20 18:30:45.615 6814-6822/net.androidbootcamp.concertproject W/art: Suspending all threads took: 25.547ms
03-20 18:30:45.932 6814-6826/net.androidbootcamp.concertproject W/art: Suspending all threads took: 10.580ms
03-20 18:30:46.021 6814-6826/net.androidbootcamp.concertproject W/art: Suspending all threads took: 9.507ms
03-20 18:30:46.026 6814-6826/net.androidbootcamp.concertproject I/art: Background partial concurrent mark sweep GC freed 454(28KB) AllocSpace objects, 0(0B) LOS objects, 34% free, 7MB/11MB, paused 11.418ms total 43.788ms
03-20 18:30:46.034 6814-6814/net.androidbootcamp.concertproject D/AndroidRuntime: Shutting down VM
03-20 18:30:46.035 6814-6814/net.androidbootcamp.concertproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: net.androidbootcamp.concertproject, PID: 6814
                                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{net.androidbootcamp.concertproject/net.androidbootcamp.concertproject.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
                                                                                      at net.androidbootcamp.concertproject.MainActivity.onCreate(MainActivity.java:28)
                                                                                      at android.app.Activity.performCreate(Activity.java:6237)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
03-20 18:30:49.459 6814-6814/? I/Process: Sending signal. PID: 6814 SIG: 9

this is the my main activity code

package net.androidbootcamp.concertproject;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
double concertPrice = 12.5;
double numberEntered;
double totalPrice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    getSupportActionBar().setDisplayUseLogoEnabled(true);

    final EditText number = (EditText) findViewById(R.id.txtNumber);

    final RadioButton maiden = (RadioButton) findViewById(R.id.radIronMaiden);
    final RadioButton fall = (RadioButton) findViewById(R.id.radFall);
    final RadioButton mastodon = (RadioButton) findViewById(R.id.radMastodon);
    final RadioButton opinion = (RadioButton) findViewById(R.id.radOther);
    final TextView result = (TextView) findViewById(R.id.txtResult);

    Button convert = (Button) findViewById(R.id.btnPrice);

    convert.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            numberEntered = Double.parseDouble(number.getText().toString());
            DecimalFormat hundred = new DecimalFormat("###.##");

            //the try catch exception
            try {

                if (maiden.isChecked() && numberEntered >= 1) {
                    totalPrice = numberEntered / concertPrice;
                    result.setText(hundred.format(totalPrice) + " is the total price" + " and have fun");
                } else if (fall.isChecked() && numberEntered >= 1) {
                    totalPrice = numberEntered / concertPrice;
                    result.setText(hundred.format(totalPrice) + " is the total price" + " and have fun");
                } else if (mastodon.isChecked() && numberEntered >= 1) {
                    totalPrice = numberEntered / concertPrice;
                    result.setText(hundred.format(totalPrice) + " is the total price" + " and have fun");
                } else if (opinion.isChecked() && numberEntered >= 1) {
                    totalPrice = numberEntered / concertPrice;
                    result.setText(hundred.format(totalPrice) + " is the total price" +
                            " and since you picked other you could go see the misfits or megadeth" +
                            " or go to other events that are provided");
                } else {
                    Toast.makeText(MainActivity.this, " sorry it has to be bigger than the numbererd you entered", Toast.LENGTH_LONG).show();
                }
            }
            catch (Exception e)
            {
                Toast.makeText(MainActivity.this, " Sorry it has to be a number", Toast.LENGTH_LONG).show();

            }



        }
    });

  }
}

and this is the xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 app:layout_behavior="@string/appbar_scrolling_view_behavior"
 tools:context="net.androidbootcamp.concertproject.MainActivity">

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/txtTitle"
     android:id="@+id/txtTitle"
     android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:textSize="20sp"
    android:textColor="#ff0000" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/txtNumber"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:text="@string/hint" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/txtNumber"
    android:layout_alignParentStart="true">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioIronMaiden"
        android:id="@+id/radIronMaiden"
        android:checked="false"
        android:textSize="25sp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioMastodon"
        android:checked="false"
        android:id="@+id/radMastodon"
        android:textSize="25sp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioShadow"
        android:id="@+id/radFall"
        android:checked="false"
        android:textSize="25sp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioOther"
        android:id="@+id/radOther"
        android:checked="false"
        android:textSize="25sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnPrice"
        android:id="@+id/btnPrice"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/rock"
        android:id="@+id/imgRock"
        android:baselineAlignBottom="false"
        android:contentDescription="@string/imgRock" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtResult"
        android:textSize="20sp"
        android:textColor="#ff0000" />

  </RadioGroup>
</RelativeLayout>
3
  • Looks like you're using a theme that specifies no ActionBar. Check your manifest for the themes you're using.
    – Mike M.
    Mar 21, 2016 at 1:04
  • you have NullpointerException, you try to use object null, you need use the debug [-1] – David Hackro just now edit Mar 21, 2016 at 2:03
  • Possible duplicate of Null pointer exception on ActionBar at FragmentActivity
    – user6011162
    Mar 21, 2016 at 4:21

2 Answers 2

0

It tells u right there in the logs.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference at net.androidbootcamp.concertproject.MainActivity.onCreate(MainActivity.java:28)

You need to perform a findViewbyId on the toolbar object and associate it to an element from your XML before you can perform getSupportActionBar ().

 Toolbar toolbar = (Toolbar) findviewbyid (R.id.yourtoolbar)
1
  • Needless to say, you need to include a Toolbar in your xml file before you can associate it in the java code.
    – deefunkt
    Mar 21, 2016 at 4:15
0

Add Toolbar in your layout.
XML:

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

    </android.support.v7.widget.Toolbar>

Add below code in your activity file.

Toolbar toolbar = (Toolbar) findviewbyid (R.id.yourtoolbar)
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.