应用程序关闭

时间:2017-05-04 11:16:55

标签: java android nullpointerexception

所以我想实现一个ProgressBar并将其设置为100%但不知何故它会在模拟器中不断崩溃。

继承代码(MainActivity.java):

package com.example.schwarzerritter.remotec;

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

public class MainScreen extends AppCompatActivity {

    ProgressBar fuelBar;




    public Button lDoor;

    public void lDoorPageOpen() {
        lDoor = (Button) findViewById(R.id.lDoor);//weist unseren Button lDoor in Java zu
        lDoor.setOnClickListener(new View.OnClickListener() {//
            @Override
            public void onClick(View v) {
                Intent lDoorPage = new Intent(MainScreen.this, lDoorPageOpen.class);//deklarieren eines intents
                startActivity(lDoorPage);//starte intent lDoorPage
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        fuelBar.setProgress(100);
        lDoorPageOpen();
        fuelBar =(ProgressBar)findViewById(R.id.fuelB);

    }
}

这是.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.schwarzerritter.remotec.MainScreen"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RemoteC"
        android:id="@+id/remoteC"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="16dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/lDoor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:text="LOCK"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="@+id/remoteC"
        android:layout_marginTop="103dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="@+id/acB"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/fuelB"
        android:layout_width="94dp"
        android:layout_height="50dp"
        android:fontFamily="monospace"
        android:text="FUEL"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="@+id/locB"
        android:layout_marginEnd="41dp"
        app:layout_constraintRight_toRightOf="@+id/locB"
        android:layout_marginTop="70dp"
        app:layout_constraintTop_toBottomOf="@+id/remoteC"
        android:layout_marginBottom="65dp"
        android:layout_marginRight="41dp" />

    <Button
        android:id="@+id/acB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:text="AC"
        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="48dp"
        android:layout_marginTop="19dp"
        app:layout_constraintTop_toBottomOf="@+id/lDoor"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="48dp" />

    <Button
        android:id="@+id/locB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:text="LOCATION"
        app:layout_constraintBottom_toBottomOf="@+id/fuelB"
        app:layout_constraintTop_toTopOf="@+id/acB"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintTop_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.859" />

    <Button
        android:id="@+id/engB"
        android:layout_width="0dp"
        android:layout_height="49dp"
        android:fontFamily="monospace"
        android:text="ENGINE START"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="35dp"
        app:layout_constraintTop_toBottomOf="@+id/locB"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp" />

    <ProgressBar
        android:id="@+id/fuelB"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="370dp"
        android:layout_height="42dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="310dp" />



</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

首先初始化进度条,然后使用setProgress(100)。

fuelBar =(ProgressBar)findViewById(R.id.fuelB);
lDoorPageOpen();
fuelBar.setProgress(100);

答案 1 :(得分:0)

检查代码的顺序,必须在设置进度之前初始化进度条。