java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.hometools / com.example.hometools.HometoolsActivity

时间:2015-06-20 18:38:07

标签: java android eclipse android-intent

我在尝试运行项目时收到错误消息。这是一项大学任务,很简单。我相信XML是可以的。

如果您需要其他任何帮助我,请告诉我,我只是一个初学者。

package com.example.hometools;
import com.example.hometools.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.CheckBox;

public class HometoolsActivity extends Activity {

    //Tela Principal

    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;

    //tinta
    EditText editText1t1;   
    EditText editText2t1;
    EditText editText3t1;
    EditText editText4t1;
    Button button1t1;
    Button button2t1; // voltar

    //piso
    EditText editText1t2;   
    EditText editText2t2;
    EditText editText3t2;
    Button button1t2;
    Button button2t2; // voltar

    //btu
    EditText editText1t3;   
    EditText editText2t3;
    EditText editText3t3;
    EditText editText4t3;
    CheckBox checkBox1t3;
    Button button1t3;
    Button button2t3; // voltar

    //aquecedor
    EditText editText1t4;   
    EditText editText2t4;
    EditText editText3t4;
    EditText editText4t4;
    EditText editText5t4;
    EditText editText6t4;
    Button button1t4;
    Button button2t4; // voltar

    //caixa
    EditText editText1t5;   
    EditText editText2t5;
    EditText editText3t5;
    Button button1t5;
    Button button2t5; // voltar

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            controlePrincipal();
    }

    public void controlePrincipal() {   
        setContentView(R.layout.activity_hometools);
        controleTinta(); //controla tinta
        controlePiso(); //controla piso
        controleBtu(); // controla btu
        controleAquecedor(); // controla aquecedor
        controleCaixa(); // controla caixa
    }

    public void controleTinta(){

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener (new OnClickListener() {

        @Override
        public void onClick(View arg0){

        setContentView(R.layout.tinta);

                editText1t1 = (EditText) findViewById(R.id.editText1t1);
                editText2t1 = (EditText) findViewById(R.id.editText2t1);
                editText3t1 = (EditText) findViewById(R.id.editText3t1);
                editText4t1 = (EditText) findViewById(R.id.editText4t1);
                button1t1 = (Button) findViewById(R.id.button1t1);

                button1t1.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){

                        double rendimento, quantidade,demao, area, valor_arred;

                        //entrada de dados
                        area=Double.parseDouble(editText1t1.getText().toString())   ;
                        demao=Double.parseDouble(editText2t1.getText().toString());
                        rendimento=Double.parseDouble(editText3t1.getText().toString());



                        quantidade = area*demao/rendimento;

                        quantidade = quantidade*1.1;

                        //arredondando o valor para cima

                        valor_arred = Math.ceil(quantidade);

                                //saída de dados

                        editText4t1.setText(String.valueOf(valor_arred));
                    }
                });
                button2t1.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){
                        controlePrincipal();
                    }
                });

    }
        });
    }

    public void controlePiso(){

        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener (new OnClickListener() {

        @Override
        public void onClick(View arg0){

        setContentView(R.layout.piso);

                editText1t2 = (EditText) findViewById(R.id.editText1t2);
                editText2t2 = (EditText) findViewById(R.id.editText2t2);
                editText3t2 = (EditText) findViewById(R.id.editText3t2);
                button1t2 = (Button) findViewById(R.id.button1t2);

                button1t2.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){

                        double area, tamanho, piso, valor_arred;

                        //entrada de dados
                        area=Double.parseDouble(editText1t2.getText().toString())   ;
                        tamanho=Double.parseDouble(editText2t2.getText().toString());



                        piso=area/tamanho;
                        piso=piso*1.1;

                        //arredondando o valor para cima.

                        valor_arred = Math.ceil(piso);
                                                        //saída de dados

                        editText3t2.setText(String.valueOf(valor_arred));
                    }
                });
                button2t2.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){
                        controlePrincipal();
                    }
                });
        }
        });

    }
    public void controleBtu(){

        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener (new OnClickListener() {

        @Override
        public void onClick(View arg0){

        setContentView(R.layout.btu);

        editText1t3 = (EditText) findViewById(R.id.editText1t3);
        editText2t3 = (EditText) findViewById(R.id.editText2t3);
        editText3t3 = (EditText) findViewById(R.id.editText3t3);
        editText4t3 = (EditText) findViewById(R.id.editText4t3);
        button1t3 = (Button) findViewById(R.id.button1t3);
        checkBox1t3 = (CheckBox) findViewById(R.id.checkBox1t3);

                button1t3.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){

                        double area, valor_arred, BTU;
                        int  pessoas, eletronicos;


                        //entrada de dados
                        area=Double.parseDouble(editText1t3.getText().toString())   ;
                        pessoas=Integer.parseInt(editText2t3.getText().toString());
                        eletronicos=Integer.parseInt(editText3t3.getText().toString());


                                //saída de dados
                        if (checkBox1t3.isChecked()) {
                        BTU=area*800+pessoas*800+eletronicos*800; 
                        }
                        else {
                            BTU=area*600+pessoas*600+eletronicos*600;
                    }
                        valor_arred=Math.ceil(BTU); 

                        editText4t3.setText(String.valueOf(valor_arred));
                    }
                });
                button2t3.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){
                        controlePrincipal();
                    }
                });
        }
        });

    }

    public void controleAquecedor(){

        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener (new OnClickListener() {

        @Override
        public void onClick(View arg0){

        setContentView(R.layout.aquecedor);

        editText1t4 = (EditText) findViewById(R.id.editText1t4);
        editText2t4 = (EditText) findViewById(R.id.editText2t4);
        editText3t4 = (EditText) findViewById(R.id.editText3t4);
        editText4t4 = (EditText) findViewById(R.id.editText4t4);
        editText5t4 = (EditText) findViewById(R.id.editText5t4);
        editText6t4 = (EditText) findViewById(R.id.editText6t4);
        button1t4 = (Button) findViewById(R.id.button1t4);


                button1t4.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){

                        int a,b,c,d;
                        double qnec,vazao,vazaoaq,quant,quantarr;

                        //entrada de dados

                        a=Integer.parseInt(editText1t4.getText().toString())    ;
                        b=Integer.parseInt(editText2t4.getText().toString())    ;
                        c=Integer.parseInt(editText3t4.getText().toString())    ;
                        d=Integer.parseInt(editText4t4.getText().toString())    ;
                        vazaoaq=Double.parseDouble(editText5t4.getText().toString())    ;




                        vazao=12*a+6*b+9*c+15*d;
                        qnec=vazao/2;
                        quant=qnec/vazaoaq;
                        quantarr=Math.ceil(quant);

                            //saída de dados

                        editText6t4.setText(String.valueOf(quantarr));
                }
                });
                button2t4.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){
                        controlePrincipal();
                    }
                });
        }
        });

    }

    public void controleCaixa(){

        button1t5 = (Button) findViewById(R.id.button1t5);
        button1t5.setOnClickListener (new OnClickListener() {

        @Override
        public void onClick(View arg0){

        setContentView(R.layout.caixa);

                editText1t5 = (EditText) findViewById(R.id.editText1t5);
                editText2t5 = (EditText) findViewById(R.id.editText2t5);
                editText3t5 = (EditText) findViewById(R.id.editText3t5);
                button1t5 = (Button) findViewById(R.id.button1t5);

                button1t5.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){

                        double minimo, intervalo, valor_arred;
                        int pessoas;


                        //entrada de dados
                        intervalo=Double.parseDouble(editText1t5.getText().toString())  ;
                        pessoas=Integer.parseInt(editText2t5.getText().toString());




                        minimo=pessoas*intervalo*150;

                        //arredondando o valor para cima.

                        valor_arred = Math.ceil(minimo);

                        //saída de dados

                        editText3t5.setText(String.valueOf(valor_arred));
                    }
                });
                button2t5.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View arg0){
                        controlePrincipal();
                    }
                });

    }
        });
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
tools:context="${relativePackage}.${activityClass}" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginRight="16dp"
    android:text="Hometools por UVA - 2015" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="57dp"
    android:text="Tinta" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="37dp"
    android:text="Piso" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="BTU" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button3"
    android:layout_below="@+id/button3"
    android:layout_marginTop="46dp"
    android:text="Aquecedor" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="32dp"
    android:text="Caixa d´água" />

</RelativeLayout>

这是我的logcat:

06-20 14:11:27.560: D/AndroidRuntime(824): Shutting down VM
06-20 14:11:27.560: W/dalvikvm(824): threadid=1: thread exiting with uncaught exception (group=0xb2a68ba8)
06-20 14:11:27.580: E/AndroidRuntime(824): FATAL EXCEPTION: main
06-20 14:11:27.580: E/AndroidRuntime(824): Process: com.example.hometools, PID: 824
06-20 14:11:27.580: E/AndroidRuntime(824): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hometools/com.example.hometools.HometoolsActivity}: java.lang.NullPointerException
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.os.Handler.dispatchMessage(Handler.java:102)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.os.Looper.loop(Looper.java:136)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.ActivityThread.main(ActivityThread.java:5001)
06-20 14:11:27.580: E/AndroidRuntime(824):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 14:11:27.580: E/AndroidRuntime(824):  at java.lang.reflect.Method.invoke(Method.java:515)
06-20 14:11:27.580: E/AndroidRuntime(824):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-20 14:11:27.580: E/AndroidRuntime(824):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-20 14:11:27.580: E/AndroidRuntime(824):  at dalvik.system.NativeStart.main(Native Method)
06-20 14:11:27.580: E/AndroidRuntime(824): Caused by: java.lang.NullPointerException
06-20 14:11:27.580: E/AndroidRuntime(824):  at com.example.hometools.HometoolsActivity.controleCaixa(HometoolsActivity.java:294)
06-20 14:11:27.580: E/AndroidRuntime(824):  at com.example.hometools.HometoolsActivity.controlePrincipal(HometoolsActivity.java:75)
06-20 14:11:27.580: E/AndroidRuntime(824):  at com.example.hometools.HometoolsActivity.onCreate(HometoolsActivity.java:66)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.Activity.performCreate(Activity.java:5231)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-20 14:11:27.580: E/AndroidRuntime(824):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-20 14:11:27.580: E/AndroidRuntime(824):  ... 11 more
06-20 14:12:37.752: I/Process(824): Sending signal. PID: 824 SIG: 9

1 个答案:

答案 0 :(得分:0)

你的错误在这里,这是一个简单的错字:

public void controleCaixa(){

  button1t5 = (Button) findViewById(R.id.button1t5);
  button1t5.setOnClickListener (new OnClickListener() {...}
  ...
}

应该是R.id.button5而不是R.id.button1t5

public void controleCaixa(){

  button5 = (Button) findViewById(R.id.button5);
  button5.setOnClickListener (new OnClickListener() {...}
  ...
} 

查看日志,很清楚:

  

引起:java.lang.NullPointerException 06-20 14:11:27.580:   E / AndroidRuntime(824):at   com.example.hometools.HometoolsActivity.controleCaixa(HometoolsActivity.java:294)   06-20 14:11:27.580:E / AndroidRuntime(824):