简体   繁体   中英

Button click on second layout doesn't work

What I want is: Press a button and then the app goes to another layout ( page). On this page you press a button and then some text is shown in a textbox. I made a activity for each layout.

I get this error E/AndroidRuntime(862): java.lang.IllegalStateException: Could not find a method calculateKfactor(View) in the activity class tweaks.engineering.MainActivity for onClick handler on view class android.widget.Button with id 'btnCalculateKf'

This is my code on my main activity to go to the other layout this works!:)

Updated MainActivity

public void  Sheetmetal (View view){
    if (view == mbtnSheetmetal){
        setContentView(R.layout.activity_sheetmetal);
        Intent intent = new Intent(this, SheetmetalActivity.class);
        startActivity(intent); 
    }
}       

This is my code for the second activity: SheetmetalActivity

package tweaks.engineeringsheet;

import tweaks.engineering.R;
import tweaks.engineering.R.id;
import tweaks.engineering.R.layout;
import android.widget.EditText;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;

public class SheetmetalActivity extends Activity{

    //text box define
    EditText mtxtKfactor;

    //Button define
    Button mbtnCalculateKf;

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

        //connect mtxt"name" to txt"name"
        mtxtKfactor = (EditText) findViewById(R.id.txtKfactor);

        //connect mbtn"name" to btn"name"
        mbtnCalculateKf = (Button) findViewById(R.id.btnCalculateKf);
    }


    public void calculatekfactor(View view){
        if (view == mbtnCalculateKf){       

            mtxtKfactor.setText("works");
            mtxtKfactor.setTextColor(Color.GREEN);

        } //end if
    }//end class
} // end program

I suppose that the onclick of your button calls a method called calculateKfactor
Make your method public, or it won't find it.
Remember that without setting a visibility modifier you're on package visibility and not public visibility

EDIT

You say you go to the second activity (where your method resides) doing this:

 public void  Sheetmetal (View view){
    if (view == mbtnSheetmetal){
        setContentView(R.layout.activity_sheetmetal); 
        }
   }

This is changing the layout of the main activity, so when you see your button you are still in the first activity, and when you click he searches your method in mainActivity.
To start another activity follow these steps:

Intent intent = new Intent(this, YourSecondActivity.class);
startActivity(intent);

If you want to use the calculateKFactor function in main activity (via a button click) you have a few options:

1) move that function into main activity from sheet metal activity

2) if you need to use the function in both activities... for the activity that doesn't have the calculateKFactor function within it, you could have a function that handles the initial button click, then calls the public function from the second file (the function would also have to be static)..

3) Alternative method, if you need to use the function in both activities... create a parent activity that each of the two activities extend..

May also want to check your capitalization.. the error has calculate in lower case but SheetmetalActivity has the Calculate in upper case..

Do you have the second Activity in your Menifest file? This is an easy mistake to miss

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