0

我在调试响应用户输入的应用程序时遇到问题。一个只显示文本的普通应用程序可以正常工作,但是当我给一个按钮一个事件处理程序时,我会收到一条通用消息“不幸的是,'应用程序名称'已停止。”。我尝试了很多解决方案,但无济于事。我正在使用 IntelliJ IDEA 12.0.4(社区版)。很抱歉问了一个非常常见的问题,但似乎每个人的解决方案都不同。

编辑 - 新的工作代码

源代码:

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());
        }
    };
}

主要的.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 很长,所以我把它贴在这里:http ://pastebin.com/2qaY8ZJj 。每个人的Logcat都这么短我不知道为什么

4

1 回答 1

2

移动此代码

//  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);

在里面onCreate()并使用 R.id....

编辑

我编辑了你的代码

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 回答