Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 6 của 6
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Cách tạo một button mới trong lập trình Android, lỗi: "unexpected" Force close!

    đây là File Activity.java

    Mã:
    package com.myApp; import android.R.layout;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout; public class CreatDynamicButtonActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);               // edtX = (EditText)findViewById(R.id.edtX);        //x = Integer.parseInt(edtX.getText().toString());        //        //EditText edtY = (EditText)findViewById(R.id.edtY);        //y = Integer.parseInt(edtY.getText().toString());        //                final Button btnCreatButton = (Button)findViewById(R.id.btnCreatButton);        final LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1);        final Button newButton = new Button(this);                final EditText edtRong = (EditText)findViewById(R.id.edtRong);                //        final EditText edtDai = (EditText)findViewById(R.id.edtDai);                        btnCreatButton.setOnClickListener(new View.OnClickListener() {                        public void onClick(View v) {                // TODO Auto-generated method stub                final int x,y,rong,dai ;                rong = Integer.parseInt(edtRong.getText().toString());                dai = Integer.parseInt(edtDai.getText().toString());                newButton.setText("NewButton");                newButton.setWidth(rong);                newButton.setHint(dai);                layout1.addView(newButton);            }        });            }}
    File Main.XML

    Mã:
    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="vi tri x: " />    <EditText        android:id="@+id/edtX"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="vi tri y: " />    <EditText        android:id="@+id/edtY"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="chieu rong: " />    <EditText        android:id="@+id/edtRong"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Chieu Dai: " />    <EditText        android:id="@+id/edtDai"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        />    <Button        android:id="@+id/btnCreatButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="CreatButton"        />        <LinearLayout            android:id="@+id/layout1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            ></LinearLayout>></LinearLayout>
    Khi RUn thì bị lỗi "unexpected" Force close! debug thì nó chẳng nhảy vào hàm Onlisten của Button, bạn nào biét giúp mình với!

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bỏ cái dòng: final Button newButton = new Button(this); đi xem nào?

    Nếu muốn tạo button trong android bằng code thì bạn phải làm tối thiểu như thế này:

    Mã:
    Button newButton = new Button(this);newButton.setText("CongdongCViet.com");layout1.addView(newButton);
    PS: hãy học cách gửi tiêu đề rõ ràng và đưa code vào tag code nâng cao, chứ để đen ngòm thì chẳng ai muốn giúp!

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn nên tìm hiểu tài liệu lập trình java/android căn bản trước rồi hãy làm. Bạn hỏi một câu hỏi rất ngớ ngẩn như trên thì không ai muốn trả lời cho bạn => bạn sẽ mãi mãi không bao giờ có câu trả lời, mà người trả lời đọc cũng thấy ngán ngẩm.

    Một vấn đề nữa mà bạn nên học hỏi là: "Hãy đọc kỹ hướng dẫn trước khi sử dụng". Trong khi Kevin đã nói rõ rằng: Nếu muốn tạo button trong android bằng code thì bạn phải làm tối thiểu như thế này: ...

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn Import cái này:
    import android.view.View.OnClickListener;

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    code android nên dùng eclipse, đừng dùng netbeans nhé bạn [IMG]images/smilies/alert.gif[/IMG]

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    nếu muốn làm game thì vớ tạm 1 cái thư viện nào mà code , có java console là đủ chiến rồi , nó chả liên quan gì đến mấy cái control này đâu

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •