0

嗨,我对 Android ADK 相当陌生。我正在尝试将单独文件中的 SETUP 类中的方法传递到主类 KakaMainActivity 中。但是,一旦应用程序通过模拟器运行,它就会立即崩溃。请帮忙!

//KakaMainActivity 类

package com.example.kaka;

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

public class KakaMainActivity extends Activity {

    Button sendMSG1 = (Button)findViewById(R.id.button_Send1);
    Button sendMSG2 = (Button)findViewById(R.id.button_Send2);
    TextView RESULT = (TextView)findViewById(R.id.text_Result);

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

    SETUP clickAGAIN = new SETUP();
    clickAGAIN.click();
   }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.kaka_main, menu);
    return true;
   }

}

//设置类

package com.example.kaka;

import android.view.View;

public class SETUP extends KakaMainActivity {
    public void click(){
            sendMSG1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v){
                        RESULT.setText("First message HELLO WORLD!");
            }
        });

        sendMSG2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                RESULT.setText("Second message BYE BYE");
            }
        }); 
  }
}
4

3 回答 3

1

几个问题...

首先,您不应该实例化Activity这样的

SETUP clickAGAIN = new SETUP();

如果SETUP只是为了持有你的onClick(),那么你可以拥有它implements OnClickListener而不扩展任何类。请参阅此答案以获取帮助

其次,Viewslayout使用inflateror 调用setContentView(). 所以改变你KakaMainActivity

Button sendMSG1; // you can declare them here
Button sendMSG2; 
TextView RESULT; 

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

    sendMSG1 = (Button)findViewById(R.id.button_Send1);  // but initialize them here
    sendMSG2 = (Button)findViewById(R.id.button_Send2);
    RESULT = (TextView)findViewById(R.id.text_Result);

此外,您应该更改RESULTresult符合 Java 标准(不是必需的,但很好的做法)。现在它看起来像一个constant.

于 2013-09-25T17:46:43.557 回答
1

之后初始化您的视图setContentView

private Button sendMSG1; 
private Button sendMSG2;
private TextView RESULT;

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kaka_main);
sendMSG1 = (Button)findViewById(R.id.button_Send1);
sendMSG2 = (Button)findViewById(R.id.button_Send2);
RESULT = (TextView)findViewById(R.id.text_Result);
sendMSG1.setOnClickListener(new OnClickListener() {
            @Override 
            public void onClick(View v){
                    RESULT.setText("First message HELLO WORLD!");
        }
    });
sendMSG2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v){
            RESULT.setText("Second message BYE BYE");
        }
    });  
}

您也可以在此活动本身中添加点击侦听器

使用匿名内部类

  sendMSG1.setOnClickListener(new OnClickListener() {
            @Override 
            public void onClick(View v){
                    RESULT.setText("First message HELLO WORLD!");
        }
    });

你也有这个

SETUP clickAGAIN = new SETUP();
clickAGAIN.click();

然后

public class SETUP extends KakaMainActivity 

你实例化了一个你不应该做的活动类。活动有一个 ui 并由 .Activity 启动startActivtiy(new Intent(param))

于 2013-09-25T17:47:30.970 回答
0

您应该在设置布局后加载 UI 组件。

setContentView(R.layout.activity_kaka_main);
sendMSG1 = (Button)findViewById(R.id.button_Send1);
...
RESULT = (TextView)findViewById(R.id.text_Result);
于 2013-09-25T17:40:16.350 回答