2013-10-23 121 views
0

你好stackoverflow社区,简单的应用程序崩溃发布

我最近开始学习和编码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> 

的logcat:

10-23 10:43:13.230: D/AndroidRuntime(837): Shutting down VM 
10-23 10:43:13.230: W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x41465700) 
10-23 10:43:13.340: E/AndroidRuntime(837): FATAL EXCEPTION: main 
10-23 10:43:13.340: E/AndroidRuntime(837): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.CPTech.bathroomtracker/com.CPTech.bathroomtracker.MainActivity}: java.lang.NullPointerException 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.os.Looper.loop(Looper.java:137) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-23 10:43:13.340: E/AndroidRuntime(837): at java.lang.reflect.Method.invokeNative(Native Method) 
10-23 10:43:13.340: E/AndroidRuntime(837): at java.lang.reflect.Method.invoke(Method.java:525) 
10-23 10:43:13.340: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-23 10:43:13.340: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-23 10:43:13.340: E/AndroidRuntime(837): at dalvik.system.NativeStart.main(Native Method) 
10-23 10:43:13.340: E/AndroidRuntime(837): Caused by: java.lang.NullPointerException 
10-23 10:43:13.340: E/AndroidRuntime(837): at com.CPTech.bathroomtracker.MainActivity.onCreate(MainActivity.java:23) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.Activity.performCreate(Activity.java:5133) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
10-23 10:43:13.340: E/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
10-23 10:43:13.340: E/AndroidRuntime(837): ... 11 more 
+1

什么是此行MainActivity.java:23 –

+0

'10-23 10:43:13.340:E/AndroidRuntime(837):引起:java.lang.NullPointerException 10-23 10:43:13.340:E/AndroidRuntime(837):at com.CPTech.bathroomtracker.MainActivity.onCreate(MainActivity.java:23)'是追踪问题的关键。 – nhgrif

+0

请注意,将一个总计存储在一个Activity对象成员变量中并不会非常有用,因为每次重新创建Activity时都会将其清除,例如如果需要从后台删除进程以便为其他人提供资源应用程序,甚至可以像旋转手机的方向一样简单,如果您尚未将Manifest配置为忽略方向更改。 –

回答

2

它看起来像这条线

tripsNumberEditText.setText(TOTAL_TRIPS); 

是给你的NPE因为你还没有初始化它。移动初始化

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

低于这条线将是你的下一个错误

此外,您使用了错误的方法setText()。你给它一个int,它会告诉它寻找与id,它显然不会找到一个resource。你需要将它与类似

tripsNumberEditText.setText("" + TOTAL_TRIPS); 

See the overloaded methods

更改为String此外,由于你的用户之前设定onCreate()文本有一个机会给任何输入,你也可以将其设置成与

android:text="0" 

注意你的XML如果您使用硬编码字符串,像这样的Eclipse会给你这将是确定,但更好地定义VAL警告你在strings.xml

+0

砸钱。 –

0

使用这样,你访问过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); 

    } 
0

跑这条线:

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

前此订单:

tripsNumberEditText.setText(TOTAL_TRIPS);

onCreate()