Android 简单应用程序启动时崩溃

Android 简单应用程序启动时崩溃,android,launch,Android,Launch,大家好,我的社区 我最近开始学习和编写android应用程序。对于我的第一个应用程序,我决定创建一个简单的应用程序,在这个应用程序中,你按下一个按钮(当你去洗手间时),计数就会在一个可编辑的文本字段中上升。还有一个复位按钮。在模拟器中启动时,它会打开,在那里我可以看到应用程序标题,但随后意外崩溃。代码如下,请帮助: MainActivity.java: package com.CPTech.bathroomtracker; import android.os.Bundle; import an

大家好,我的社区

我最近开始学习和编写android应用程序。对于我的第一个应用程序,我决定创建一个简单的应用程序,在这个应用程序中,你按下一个按钮(当你去洗手间时),计数就会在一个可编辑的文本字段中上升。还有一个复位按钮。在模拟器中启动时,它会打开,在那里我可以看到应用程序标题,但随后意外崩溃。代码如下,请帮助:

MainActivity.java:

package com.CPTech.bathroomtracker;

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

public class MainActivity extends Activity {

    private int TOTAL_TRIPS = 0;

    private EditText tripsNumberEditText;       
    Button bathroomTripButton;
    Button resetButton;

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

        tripsNumberEditText.setText(TOTAL_TRIPS);

        bathroomTripButton = (Button) findViewById(R.id.bathroomTripButton);

        bathroomTripButton.setOnClickListener(bathroomTripButtonListener);

        resetButton = (Button) findViewById(R.id.resetButton);

        resetButton.setOnClickListener(resetButtonListener);

        tripsNumberEditText = (EditText) findViewById(R.id.tripsNumberEditText);

    }

    public OnClickListener bathroomTripButtonListener = new OnClickListener(){

        @Override
        public void onClick(View v) {

            TOTAL_TRIPS++;
            tripsNumberEditText.setText(TOTAL_TRIPS);

        }

    };

    public OnClickListener resetButtonListener = new OnClickListener(){

        @Override
        public void onClick(View v) {

            TOTAL_TRIPS = 0;
            tripsNumberEditText.setText(TOTAL_TRIPS);

        }


    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
MainActivity.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    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"
    android:background="@color/dark_blue"
    tools:context=".MainActivity" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/bathroomTripButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/ash_grey"
            android:layout_marginTop="30dp"
            android:text="@string/bathroom_trip_button"
             />


    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginTop="30dp" >

        <TextView
            android:id="@+id/todayTripsTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="20dp"
            android:text="@string/today_trips"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="16pt" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp" >

        <EditText
            android:id="@+id/tripsNumberEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="20dp"
            android:textColor="@color/white"
            android:textSize="20pt"
            android:background="@color/davy_grey"
            android:ems="2" >

            <requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="75dp" >

        <Button
            android:id="@+id/resetButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/ash_grey"
            android:text="@string/reset_count" />

    </TableRow>

</TableLayout>

像这样使用,您在实例化它之前已经访问了
tripsNumberEditText

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



        bathroomTripButton = (Button) findViewById(R.id.bathroomTripButton);

        bathroomTripButton.setOnClickListener(bathroomTripButtonListener);

        resetButton = (Button) findViewById(R.id.resetButton);

        resetButton.setOnClickListener(resetButtonListener);

        tripsNumberEditText = (EditText) findViewById(R.id.tripsNumberEditText);

        tripsNumberEditText.setText(TOTAL_TRIPS);

    }

看起来像这条线

tripsNumberEditText.setText(TOTAL_TRIPS);
正在给您的
NPE
,因为您尚未初始化它。将此行移到初始化的下方

tripsNumberEditText = (EditText) findViewById(R.id.tripsNumberEditText);
tripsNumberEditText.setText(TOTAL_TRIPS);
将是您的下一个错误

此外,您使用了错误的
setText()
方法。你给它一个
int
,它会告诉它去寻找一个
资源
,这个
id显然找不到。您需要将其更改为
字符串
,如下所示

tripsNumberEditText.setText("" + TOTAL_TRIPS);

此外,由于您是在用户有机会提供任何输入之前在
onCreate()
中设置文本,因此也可以使用

android:text="0"
注意如果您使用像这样的硬编码字符串,Eclipse会给您一个警告,这是可以的,但最好在
strings.xml中定义值

tripsNumberEditText=(EditText)findViewById(R.id.tripsNumberEditText);

在这一行之前:

tripsNumberEditText.setText(总行程)


onCreate()

这行MainActivity.java:23
10-23 10:43:13.340:E/AndroidRuntime(837):由以下原因引起:java.lang.NullPointerException 10-23 10:43:13.340:E/AndroidRuntime(837):在com.CPTech.bathroomtracker.MainActivity.onCreate(MainActivity.java:23)
是追踪问题的关键。请注意,在“活动对象”成员变量中存储总计并不是很有用,因为每次重新创建“活动”时,它都会被清除,例如,如果需要将流程从后台删除,以便为其他应用程序提供资源,或者,如果您没有将清单配置为忽略方向更改,甚至可以执行一些简单的操作,如旋转手机的方向。
android:text="0"