Iam是Android項目開發的初學者。我已經開始使用我在網上找到的教程開發一個基本的應用程序。我使用Eclipse。運行我的項目時沒有錯誤。即時我得到這個錯誤後,我點擊我的模擬器上的應用程序。下述R d XML和Java編碼,我用:我收到「不幸Myapp已停止工作」當我在模擬器中啓動Android項目
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/result"
android:textSize="30sp" />
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/add"
android:textSize="20sp" />
<Button
android:id="@+id/s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/sub"
android:textSize="20sp" /></LinearLayout>
的Java:
package com.app.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
公共類MainActivity擴展活動{
int count;
Button add,sub;
TextView disp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
count=0;
add=(Button) findViewById(R.id.a);
sub=(Button) findViewById(R.id.s);
disp=(Button) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
count=count+1;
disp.setText("Result is " + count);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
count=count-1;
disp.setText("Result is " + count);
}
});
}
}
請提出解決方案後,我應該繼續我的項目開發。
「不幸的是MYAPP已停止工作」是有意義的我們,如果你告訴使用什麼是下一行的一句話。發佈logcat如果你有錯誤。 – wtsang02
abd logcat;查找NULLPointerException - 這將指向錯誤輸出的代碼中的行。 – Booger
@Booger你在哪裏看到NPE? – wtsang02