不幸的是,Myapp已经停止了。我怎么解决这个问题?

时间:2016-03-21 00:51:56

标签: android android-studio

我正在为我的项目开发这个应用程序,但每次运行它时都会收到一条消息,说“不幸的是,ConcertProject已停止”。我不知道它有什么问题

这是我得到的错误

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

这是我的主要活动代码

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();

            }



        }
    });

  }
}

这是xml代码

<?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>

2 个答案:

答案 0 :(得分:0)

它在日志中告诉你。

  

引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(布尔值)”   在null对象引用   net.androidbootcamp.concertproject.MainActivity.onCreate(MainActivity.java:28)

您需要在工具栏对象上执行findViewbyId并将其与XML中的元素相关联,然后才能执行getSupportActionBar()。

 Toolbar toolbar = (Toolbar) findviewbyid (R.id.yourtoolbar)

答案 1 :(得分:0)

在布局中添加工具栏 的 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>

在活动文件中添加以下代码。

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