Button.setOnClickListener(本);错误错误、Button、setOnClickListener

2023-09-07 12:18:07 作者:欠一个拥抱

这是code:

package com.elfapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private Button btn_Login;
    private EditText et_UserName;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btn_Login = (Button)findViewById(R.id.button_login);
        btn_Login.setOnClickListener(this);

        et_UserName = (EditText)findViewById(R.id.editText_userName);

        setContentView(R.layout.main);
    }

    public void onClick(View v) {
        if (v.equals(btn_Login)) {
                // skriver ut en toast när man klickar på knappen
            //Toast.makeText(MainActivity.this, "Ansluter till server...", Toast.LENGTH_SHORT).show();

                // används i debuggern för att påvisa att programmet exekverat hit
            //Log.v("ThisApp", "onClick Successful");

                // TODO skickar det som står i et_UserName till controller (genom TCP/IP), som ska kolla om användaren finns
            Intent intent = new Intent(this, goListView);
            this.startActivity(intent);
        }
    }

}

程序崩溃,当我到达 btn_Login.setOnClickListener(本); 声明,我没有太大的做什么的线索。(没用过到Eclipse调试器。)

The program crashes when I reach the btn_Login.setOnClickListener(this); statement and I don't have much of a clue about what to do.. (not used to the Eclipse debugger..)

推荐答案

您按钮的初始化之前,移动的setContentView(R.layout.main)电话。这将有助于。祝你好运!

Move the setContentView(R.layout.main) call before the initializing of your button. This should help. Good luck!