-
28-04-2012, 08:01 AM #1Silver member
- 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); } }); }}
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>
View more random threads:
- Cắt 1 ảnh lớn ra n ảnh nhỏ bằng nhau ?
- Run ứng dụng Android bị lỗi launching new_configuration has encountered a problem?
- tại sao không xuất hiện màn hình xử lý trong j2me?
- Cơ chế tự update app Android là như thế nào?
- Hiển thị tần số của thiết bị bluetooth kết nối với android smartphone
- Lỗi trong file strings.xml khi tiến hành quá trình export ra file .APK
- Lỗi không thể load file mp3
- Cách chuyển activity sau khi đăng nhập app bằng acc Facebook
- Cách vẽ đồ thị 2D trong android?
- Hiển thị xung âm thanh trên công cụ nào của Android.
-
28-04-2012, 10:00 AM #2Banned
- 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);
-
28-04-2012, 10:41 AM #3Junior Member
- 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: ...
-
02-05-2012, 08:04 AM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Bạn Import cái này:
import android.view.View.OnClickListener;
-
02-05-2012, 06:29 PM #5Junior Member
- 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]
-
04-05-2012, 06:18 PM #6Junior Member
- 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
Mỗi liệu pháp tại Queen Spa là một hành trình chiều chuộng bản thân, giúp bạn tìm lại sức sống và năng lượng mới. Không gian của Queen Spa được thiết kế sao cho tối ưu hóa sự thoải mái và thư giãn...
Sống Đẹp và Khỏe Mạnh với Queen...