开发者

Android (student cw) in need of direction

开发者 https://www.devze.com 2023-04-13 07:59 出处:网络
public class Menu extends Activity{ /** Called when the activity is first created. */ public void onCreate(Bundle icicle) {
public class Menu extends Activity  {
    /** Called when the activity is first created. */



 public void onCreate(Bundle icicle) {


     //myIntent.setClassName("hello.World", "hello.World.mybuttonclick");
    // myIntent.putExtra("com.android.samples.SpecialValue", "Hello, Joe!"); // key/value pair, where key needs current package prefix.
     //startActivity(myIntent); 

        //Button myButton = (Button) findViewById(R.id.my_button);
        super.onCreate(icicle);


        setContentView(R.layout.main);
    }

 public void updateLayout(){

     Intent myIntent = new Intent(Menu.this, mybuttonclick.class);
     startActivity(myIntent);

    // TextView sayHello = (TextView) findViewById(R.id.Hello);

 }

}

Hey guys, I am a new android java student and we have to develop a simple hello world app.. I am finding some difficulty getting my onClick() activity to work, using android:Onclick in xml.. what i am trying to do is change the content view do display a simply a different layout and saying hello.. i am using setContentLayout to do this, every time i click sa开发者_JAVA百科id button tho the android app crashes out.. am i doing something wrong?

regards,

Stefan


When you set a click listener in xml you must have the method defined inside the activity you are clicking in. Lets say you set the onClick in xml to be "buttonClicked", you must create a method looking exactly like the one below.

    public void buttonClicked(View view)
    {
          //Your code here
    }

The thing to notice is that the method is a public void with only a single parameter of type View. XML defined click listeners must be like this to work. The view object in the example above is the view that was clicked.


You update layout function needs to read

public void updateLayout(View view)


In response to your question, there are a number of things that are issues causing the complication that you described. Let it first be said, that you don't have to do anything any particular way, provided that you make concessions for certain things. Android is a very flexible platform and Java, as an OOP language allows you to do things that many non OOP languages do not.

Whenever you create a "clickable" item, like a Button, if you want to have your program respond, you must have something "listen" to it. This is known as a Listener. In your case, you are looking for an OnClickListener. The OnClickListener does not have to be a part of the Activity necessarily. It just has to be a class that implements View.OnClickListener. Then, you have tell the setOnClickListener() method of the Button who its listener is. The following example shows what is necessary without your declaration in XML (but it is important).

class Menu extends Activity implements View.OnClickListener
{
     public void onCreate(Bundle icicle)
     {    setContentView(R.layout.main);
          Button btn = (Button)findViewById(R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML);
          btn.setOnClickListener(this);
     }

     public void onClick(View view)
     {    int id = view.getId();
          if (id == R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML)
              updateLayout()//Do your Click event here
     }

     public void updateLayout()
     {   //updateLayout code...
     }
}

Something that needs to be noted is the OnClick() above. Every OnClickListener must use the same signature as theOnClick() That means itmust have the same return and same arguments even if it has a different name. For what you are trying to do (in XML), you have set your android:OnClick to updateLayout. This means that `updateLayout() must be declared as follows:

public void updateLayout(View view)

Now, getting the update method to actually work: While you provide your code, we don't actually know what errors you are getting. It is always much easier to solve a problem if we have a copy of the Logcat output that includes the error you are receiving. Once, we have that we can target your error specifically and I can edit my answer to include what you may additionally need.

FuzzicalLogic

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号