简体   繁体   中英

Unable to run code for Simple Counter App for Android

I am trying to make an application on Android Studio with the help of a tutorial. I managed to get the User Interface right and I think that I have assigned the correct buttons too. I am unable to get where I am going wrong with my code. I am new to Java and so I am unable to pinpoint the error I have committed. I am posting below the code from the files I was asked to edit in the tutorial.

package com.example.to_dolistapplication.app;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity implements OnClickListener {

    Button btn1;
    Button btn2;
    Button btn3;
    TextView textTitle;
    EditText scoreText;
    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = (Button)findViewById(R.id.button);
        btn2 = (Button)findViewById(R.id.button2);
        btn3 = (Button)findViewById(R.id.button3);
        scoreText = (EditText)findViewById(R.id.textView);
        textTitle = (TextView)findViewById(R.id.editText);

        //---set on click listeners on the buttons-----
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);

        // change font size of the text
        textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
    }

    @Override
    public void onClick(View v) {
        if (v == btn1){
            counter++;
            scoreText.setText(Integer.toString(counter));
            scoreText.setBackgroundColor(Color.CYAN);
        }
        if (v == btn2){
            counter--;
            scoreText.setText(Integer.toString(counter));
            scoreText.setBackgroundColor(Color.GREEN);
        }

        if (v == btn3){
            counter = 0;
            scoreText.setText(Integer.toString(counter));
            scoreText.setBackgroundColor(Color.RED);
        }
    }

}
Above is the File from MainActivity.java



<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.to_dolistapplication.app.MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+1"
    android:id="@+id/button"
    android:onClick="@string/intro"
    android:layout_below="@+id/editText"
    android:layout_toRightOf="@+id/textView"
    android:layout_marginTop="79dp"
    android:layout_alignRight="@+id/editText"
    android:layout_alignEnd="@+id/editText" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="-1"
    android:id="@+id/button2"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_alignRight="@+id/editText"
    android:layout_alignEnd="@+id/editText" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reset"
    android:id="@+id/button3"
    android:layout_below="@+id/button2"
    android:layout_marginTop="64dp"
    android:layout_alignRight="@+id/button2"
    android:layout_alignEnd="@+id/button2"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignStart="@+id/button2" />

<TextClock
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textClock"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@+id/textView2"
    android:layout_alignBottom="@+id/textView2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="29dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Score"
    android:id="@+id/textView"
    android:layout_alignBottom="@+id/editText"
    android:layout_toLeftOf="@+id/editText" />

This is from the file activity_main.xml.

The app, when run on emulator, displays Unfortunately Counter App has stopped. what might be the reason for the app not working? Please help.

**EditText** scoreText = (EditText)findViewById(R.id.textView); **TextView** textTitle = (TextView)findViewById(R.id.editText);

you missmathed with types: android:id="@+id/editText" is EditText, but in Activity you wrote his id to TextView.

And you missmathed with types: android:id="@+id/textView" is TextView, but but in Activity you wrote his id as EditText.

You've mismatched id's in onCreate() method. Use the debug mode or just look at your console output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM