0

I am having a problem when debugging apps that respond to user input. An ordinary app that just displays text works fine, but when I give a button an event handler I get a generic message "Unfortunately, 'App Name' has stopped.". I have tried many solutions, but to no avail. I am using IntelliJ IDEA 12.0.4 (Community Edition). Sorry for asking a very common question but it seems the solution is different for everyone.

Edit - new working code

Source Code:

package com.example.Android_Test;

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

import static com.example.Android_Test.R.*;

public class MyActivity extends Activity {

    EditText editText;
    TextView textView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //  Declare all widget controls.
        editText = (EditText) findViewById(R.id.txtEdit);
        Button button = (Button) findViewById(R.id.btnChange);
        textView = (TextView) findViewById(R.id.lblText);

        button.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText(editText.getText());
        }
    };
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/layout">
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtEdit" android:layout_gravity="left|center_vertical"
            android:layout_alignParentLeft="true" android:layout_marginLeft="0dp" android:layout_alignParentTop="true"
            android:layout_marginTop="0dp" android:layout_alignParentRight="true" android:hint="Type here change text"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change Text"
            android:id="@+id/btnChange"
            android:layout_alignLeft="@+id/txtEdit" android:layout_below="@+id/txtEdit"
            android:layout_alignRight="@+id/txtEdit"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change this text"
            android:id="@+id/lblText"
            android:layout_alignLeft="@+id/txtEdit" android:layout_below="@+id/btnChange"
            android:layout_alignRight="@+id/txtEdit" android:textSize="18dp"/>
</RelativeLayout>

Logcat is very long so I pasted it here: http://pastebin.com/2qaY8ZJj. Everyone's Logcat is so much shorter i don't know why

4

1 に答える 1

2

Move this code

//  Declare all widget controls.
    EditText editText = (EditText) findViewById(R.id.txtEdit);
    Button button = (Button) findViewById(R.id.btnChange);
    TextView textView = (TextView) findViewById(R.id.lblText);

inside onCreate() and use R.id... .

Edit

I edited youre code

public class MyActivity extends Activity {

    EditText editText;
    TextView textView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //  Declare all widget controls.
         editText = (EditText) findViewById(R.id.txtEdit);
        Button button = (Button) findViewById(R.id.btnChange);
        textView = (TextView) findViewById(R.id.lblText);

        button.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText(editText.getText().toString);
        }
    };
}
于 2013-03-24T23:28:24.987 に答える