Android App:无法将方法从另一个类传递到主类

时间:2013-09-25 17:35:30

标签: java android

大家好,我是Android ADK的新手。我试图将方法从单独的SETUP类文件传递到主要类,即KakaMainActivity。但是,只要应用程序通过模拟器运行,它就会立即崩溃。请帮忙!

// KakaMainActivity Class

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

}

// SETUP Class

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

3 个答案:

答案 0 :(得分:1)

有几个问题......

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

SETUP clickAGAIN = new SETUP();

如果SETUP仅用于保留您的onClick(),那么您可以拥有implements OnClickListener而不会延长任何课程。 See this answer for help with that

其次,在您使用Views或通过调用layout夸大inflater之前,您无法尝试实例化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);

此外,您应该将RESULT更改为result以符合Java标准(不是必需的,但是很好的做法)。现在它看起来像constant

答案 1 :(得分: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,由startActivtiy(new Intent(param))启动。

答案 2 :(得分:0)

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

setContentView(R.layout.activity_kaka_main);
sendMSG1 = (Button)findViewById(R.id.button_Send1);
...
RESULT = (TextView)findViewById(R.id.text_Result);