[英]Android App: Cannot pass method from another class into the main class
Hi there I am fairly new to Android ADK. 大家好,我是Android ADK的新手。 I am trying to pass the method from the class SETUP which is on seperate file into the main class which is KakaMainActivity. 我试图将方法从单独的SETUP类文件传递到主要类,即KakaMainActivity。 However as soon as the app runs via simulator it crashes right away. 但是,只要应用程序通过模拟器运行,它就会立即崩溃。 Please help! 请帮忙!
//KakaMainActivity Class // 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 // SETUP类
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");
}
});
}
}
Several problems... 几个问题......
First, you should not instantiate an Activity
like this 首先,您不应该像这样实例化一个Activity
SETUP clickAGAIN = new SETUP();
If SETUP
is only to hold your onClick()
then you can have it implements OnClickListener
and not extend any class. 如果SETUP
仅用于保存onClick()
那么您可以让它implements OnClickListener
而不扩展任何类。 See this answer for help with that 请参阅此答案以获取帮助
Second, you can't try to instantiate Views
until you have inflated your layout
with an inflater
or by calling setContentView()
. 其次,在使用inflater
夸大layout
或调用setContentView()
之前,不能尝试实例化Views
。 So change your KakaMainActivity
to 所以将你的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);
Also, you should change RESULT
to result
to conform to Java standards (not necessary but good practice). 此外,您应该将RESULT
更改为result
以符合Java标准(不是必需的,但是良好实践)。 Right now it looks like a constant
. 现在它看起来像一个constant
。
Initialize your views after setContentView
在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");
}
});
}
Also you can add click listeners in this activity itself 您还可以在此活动本身中添加单击侦听器
Using annonymous inner class 使用匿名内部类
sendMSG1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
RESULT.setText("First message HELLO WORLD!");
}
});
Also you have this 你也有这个
SETUP clickAGAIN = new SETUP();
clickAGAIN.click();
Then 然后
public class SETUP extends KakaMainActivity
Your instantiating a activity class which you should not do .Activity has a ui and is started by startActivtiy(new Intent(param))
. 你实例化一个你不应该做的活动类.Activity有一个ui,由startActivtiy(new Intent(param))
。
You should load the UI component after setting up the layout. 您应该在设置布局后加载UI组件。
setContentView(R.layout.activity_kaka_main);
sendMSG1 = (Button)findViewById(R.id.button_Send1);
...
RESULT = (TextView)findViewById(R.id.text_Result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.