简体   繁体   中英

Android App: Cannot pass method from another class into the main class

Hi there I am fairly new to 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. However as soon as the app runs via simulator it crashes right away. Please help!

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

Several problems...

First, you should not instantiate an Activity like this

SETUP clickAGAIN = new SETUP();

If SETUP is only to hold your onClick() then you can have it implements OnClickListener and not extend any class. 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() . So change your KakaMainActivity to

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). Right now it looks like a constant .

Initialize your views after 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)) .

You should load the UI component after setting up the layout.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM