0

我想设计一个有 4 个按钮的应用程序,每个按钮的值从 1 到 4 和空格按钮。单击按钮时,它会在 textview 中给出一个值。在模拟器上运行时,它会给出错误“应用程序意外停止”。java代码如下:

package com.android.tusway;

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.TextView;


public class TapkeysActivity extends Activity  {
/** Called when the activity is first created. */


private Button b1,b2,b3,b4,bspace;
private String stringDb;
private TextView tv;


private void updateTextField() {
    tv.setText(this.stringDb);


}


@Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        // adjusting key regarding window sizes

        et = (EditText) findViewById(R.id.editText1);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        bspace = (Button) findViewById(R.id.btspace);

        tv = (TextView) findViewById(R.id.textView1);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stringDb += "1";
                updateTextField();  

            }
        });


        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stringDb += "2";
                updateTextField();  

            }
        });

        b3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stringDb += "3";
                updateTextField();  

            }
        });

        b4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stringDb += "4";
                updateTextField();  

            }
        });

        bspace.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stringDb += " ";
                updateTextField();  

            }
        });

}
    }

Main.xml 布局

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <EditText android:layout_height="wrap_content" android:layout_width="match_parent"     android:id="@+id/editText1">
    <requestFocus></requestFocus>
</EditText>
<TextView  android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="100dp" android:text="1"></Button>
<Button android:text="2" android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="97dp"></Button>
<Button android:text="3" android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="96dp"></Button>
<Button android:text="4" android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="97dp"></Button>
<Button android:id="@+id/btspace" android:layout_height="wrap_content" android:text="Space" android:layout_width="match_parent"></Button>
</LinearLayout>

日志猫:

01-17 00:23:50.069: ERROR/AndroidRuntime(1337): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.tusway/com.android.tusway.TapkeysActivity}: java.lang.NullPointerException
01-17 00:23:50.069: ERROR/AndroidRuntime(1337):     at com.android.tusway.TapkeysActivity.onCreate(TapkeysActivity.java:86)

日志猫:

01-17 00:12:31.319: DEBUG/PackageManager(54): Scanning package com.android.tusway
01-17 00:12:31.389: INFO/PackageManager(54): /data/app/com.android.tusway-1.apk changed; unpacking
01-17 00:12:31.469: DEBUG/installd(34): DexInv: --- BEGIN '/data/app/com.android.tusway-1.apk' ---
01-17 00:12:32.820: DEBUG/installd(34): DexInv: --- END '/data/app/com.android.tusway-1.apk' (success) ---
01-17 00:12:32.829: DEBUG/PackageManager(54):   Activities: com.android.tusway.TapkeysActivity
01-17 00:12:32.849: INFO/ActivityManager(54): Force stopping package com.android.tusway uid=10043
01-17 00:12:33.639: INFO/installd(34): move /data/dalvik-cache/data@app@com.android.tusway-1.apk@classes.dex -> /data/dalvik-cache/data@app@com.android.tusway-1.apk@classes.dex
01-17 00:12:33.659: DEBUG/PackageManager(54): New package installed in /data/app/com.android.tusway-1.apk
01-17 00:12:49.059: INFO/ActivityManager(54): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.android.tusway/.TapkeysActivity }
01-17 00:12:50.719: INFO/ActivityManager(54): Start proc com.android.tusway for activity com.android.tusway/.TapkeysActivity: pid=1307 uid=10043 gids={}
01-17 00:12:53.663: ERROR/AndroidRuntime(1307): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.tusway/com.android.tusway.TapkeysActivity}: java.lang.NullPointerException
01-17 00:12:53.663: ERROR/AndroidRuntime(1307):     at com.android.tusway.TapkeysActivity.onCreate(TapkeysActivity.java:86)
01-17 00:12:53.769: WARN/ActivityManager(54):   Force finishing activity com.android.tusway/.TapkeysActivity
01-17 00:12:54.629: WARN/ActivityManager(54): Activity pause timeout for HistoryRecord{44f94e30 com.android.tusway/.TapkeysActivity}
01-17 00:13:04.169: INFO/ActivityManager(54): Process com.android.tusway (pid 1307) has died.
01-17 00:13:08.790: WARN/ActivityManager(54): Activity destroy timeout for HistoryRecord{44f94e30 com.android.tusway/.TapkeysActivity}
01-17 00:23:18.783: INFO/PackageManager(54): Removing non-system package:com.android.tusway
01-17 00:23:18.791: INFO/ActivityManager(54): Force stopping package com.android.tusway uid=10043
01-17 00:23:20.010: DEBUG/PackageManager(54): Scanning package com.android.tusway
01-17 00:23:20.060: INFO/PackageManager(54): Package com.android.tusway codePath changed from /data/app/com.android.tusway-1.apk to /data/app/com.android.tusway-2.apk; Retaining data and using new
01-17 00:23:20.118: INFO/PackageManager(54): /data/app/com.android.tusway-2.apk changed; unpacking
01-17 00:23:20.200: DEBUG/installd(34): DexInv: --- BEGIN '/data/app/com.android.tusway-2.apk' ---
01-17 00:23:21.969: DEBUG/installd(34): DexInv: --- END '/data/app/com.android.tusway-2.apk' (success) ---
01-17 00:23:22.000: INFO/ActivityManager(54): Force stopping package com.android.tusway uid=10043
01-17 00:23:22.009: WARN/PackageManager(54): Code path for pkg : com.android.tusway changing from /data/app/com.android.tusway-1.apk to /data/app/com.android.tusway-2.apk
01-17 00:23:22.119: WARN/PackageManager(54): Resource path for pkg : com.android.tusway changing from /data/app/com.android.tusway-1.apk to /data/app/com.android.tusway-2.apk
01-17 00:23:22.199: DEBUG/PackageManager(54):   Activities: com.android.tusway.TapkeysActivity
01-17 00:23:23.219: INFO/installd(34): move /data/dalvik-cache/data@app@com.android.tusway-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.android.tusway-2.apk@classes.dex
01-17 00:23:23.219: DEBUG/PackageManager(54): New package installed in /data/app/com.android.tusway-2.apk
01-17 00:23:23.219: INFO/ActivityManager(54): Force stopping package com.android.tusway uid=10043
01-17 00:23:29.479: INFO/installd(34): unlink /data/dalvik-cache/data@app@com.android.tusway-1.apk@classes.dex
01-17 00:23:44.229: INFO/ActivityManager(54): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.android.tusway/.TapkeysActivity }
01-17 00:23:46.030: INFO/ActivityManager(54): Start proc com.android.tusway for activity com.android.tusway/.TapkeysActivity: pid=1337 uid=10043 gids={}
01-17 00:23:50.069: ERROR/AndroidRuntime(1337): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.tusway/com.android.tusway.TapkeysActivity}: java.lang.NullPointerException
01-17 00:23:50.069: ERROR/AndroidRuntime(1337):     at com.android.tusway.TapkeysActivity.onCreate(TapkeysActivity.java:86)
01-17 00:23:50.179: WARN/ActivityManager(54):   Force finishing activity com.android.tusway/.TapkeysActivity
01-17 00:23:51.263: WARN/ActivityManager(54): Activity pause timeout for HistoryRecord{44fc8bf8 com.android.tusway/.TapkeysActivity}
01-17 00:24:06.806: WARN/ActivityManager(54): Activity destroy timeout for HistoryRecord{44fc8bf8 com.android.tusway/.TapkeysActivity}
01-17 00:28:52.774: INFO/ActivityManager(54): Process com.android.tusway (pid 1337) has died.
4

1 回答 1

1

你没有启动你的stringDB,你可以把它放在0

于 2013-01-16T19:29:19.607 回答