0

我是初学者,我需要一些建议。当我尝试在 android 模拟器中运行以下 Android 应用程序时,当我尝试运行它时,它会显示“不幸的是,再次关闭”。

我不确定为什么它不起作用。

有人可以给我建议吗?谢谢马特

爪哇:

package again.we.practice;

import org.apache.cordova.DroidGap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AgainActivity extends DroidGap {
/** Called when the activity is first created. */

int counter;
Button add, sub;
TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView (R.layout.main);
    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    add = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.tvdisplay);
    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Your total is " + counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("Your total is " + counter);
        }
    });
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="again.we.practice"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7"/>
<supports-screens 
android:largeScreens="true" 
android:normalScreens="true" 
android:smallScreens="true" 
android:resizeable="true" 
android:anyDensity="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name" 
        android:name=".AgainActivity"
        android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
4

2 回答 2

2

sub 从未初始化,因此给出一个空指针

sub = (Button) findViewById(R.id.bSub);

会成功的

于 2012-05-28T23:49:38.117 回答
0

代替

add = (Button) findViewById(R.id.bSub);

sub = (Button) findViewById(R.id.bSub);

你得到一个空指针异常,因为 sub 没有实例化。如果错误没有消失,请打印 log cat,以便我们为您提供帮助。

于 2012-05-29T00:01:55.567 回答