amir R-ian
amir R-ian

Reputation: 61

force close simple android coding

I've had this problem for a week now and since it doesn't have any actual errors in java file i couldn't search for an answer.when i try to run the app it forcibly closes.by the way i am really new to programming so excuse me if it's an stupid question. any way lets get to the app, i want it to show the time on a text-field when the button is pressed.and here is my code

package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Date;

public class MainActivity extends Activity implements View.OnClickListener {
    Button btn;
@Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);
    btn=(Button)findViewById(R.id.button1);
    EditText txt=(EditText)findViewById(R.id.text);
    txt.setText("welcome,press the button to show the time");
    btn.setOnClickListener(this);
    }
    public void onClick(View view) {
        updateTime();
        }
    private void updateTime() {
        EditText txt=(EditText)findViewById(R.id.text);
        txt.setText(new Date().toString());
    }
}

and here is the layout if you need

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp"
        android:text="Press" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="88dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Upvotes: 1

Views: 67

Answers (2)

user3956566
user3956566

Reputation:

    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_test);
    btn=(Button)findViewById(R.id.button1);
    EditText txt=(EditText)findViewById(R.id.text);
    txt.setText("welcome,press the button to show the time");
   // btn.setOnClickListener(this);
}

In your xml:

 <Button
    android:id="@+id/button1"
    ....
    android:onClick="onClick"/>

Setting the onClick in the xml will call that method when the button is clicked.

Also match up the xml and java element to be either both an EditText or TextView.

Also you don't need to create two EditText txt. You are creating two local variables, when you can create one.

Button btn;
EditText txt;
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_test);
    btn=(Button)findViewById(R.id.button1);
    txt=(EditText)findViewById(R.id.text);
    txt.setText("welcome,press the button to show the time");
   // btn.setOnClickListener(this);
}
public void onClick(View view) {
    updateTime();
}
private void updateTime() {
    txt.setText(new Date().toString());
}

Upvotes: 0

Jorgesys
Jorgesys

Reputation: 126523

Your are using in your layout a Textview not an EditextView

<TextView
        android:id="@+id/text"

This line is your problem:

EditText txt=(EditText)findViewById(R.id.text);

you will change this line to:

  TextView txt=(TextView)findViewById(R.id.text);

but what do you need a EditextView or a TextView?

Your code must be something like:

public class MainActivity extends Activity implements View.OnClickListener {
    Button btn;
    TextView txt;

@Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);
    btn=(Button)findViewById(R.id.button1);
    txt=(TextView)findViewById(R.id.text);
    txt.setText("welcome,press the button to show the time");
    btn.setOnClickListener(this);
    }
    public void onClick(View view) {
        updateTime();
        }
    private void updateTime() {            
        txt.setText(new Date().toString());
    }
}

Upvotes: 2

Related Questions