簡體   English   中英

不幸的是,Android應用已停止

[英]android app has unfortunately stopped

我正在嘗試在android模擬器上運行此應用,但顯示“不幸的是,該應用已停止工作”:

package com.example.myapp;

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

public class MainActivity extends Activity {

    static int counter;
    static Button badd, bsub;
    static TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter=0;
        badd=(Button)findViewById(R.id.button1);
        bsub=(Button)findViewById(R.id.button2);
        text=(TextView) findViewById(R.id.textView1);

        badd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                text.setText("your total is :"+counter);
            }
        });


        bsub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                text.setText("your total is :"+counter);



            }
        });


    }

    @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;
    }

}

xml代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="your total is 0"
        android:textSize="65sp"

         />

    <Button
        android:id="@+id/button1"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_gravity="center"
        android:text="add"
           />

    <Button
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="117dp"
        android:text="subtract" />

</RelativeLayout>

日志貓顯示以下內容:

caused by java.lang.nullpointexception

在第二個Button中添加android:id =“ @ + id / button2”

<Button
    android:id="@+id/button2"
    android:layout_width="100sp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="117dp"
    android:text="subtract" />

問題沒有給出任何“未定義的ID”錯誤,是第二個按鈕視圖中的以下代碼行。

android:layout_alignLeft="@+id/button2"

盡管您已經使用“ @ + id”聲明了它,但是它將創建一個新的ID。 並且不會在您的活動代碼中給出任何錯誤。 用我上面寫的代碼更改該代碼。

對於第二個按鈕,添加

android:id="@+id/button2"

並改變

android:layout_alignLeft="@+id/button1"

android:layout_alignLeft="@id/button1"

如下更改布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="your total is 0"
        android:textSize="65sp"

         />

    <Button
        android:id="@+id/button1"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_gravity="center"
        android:text="add"
           />

    <Button
        android:id="@+id/button2"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/button1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="117dp"
        android:text="subtract" />

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM