我想根据计数动态显示单选按钮,但我无法显示。
这是我的源代码
public class PeriodChange extends Activity implements OnClickListener {
/**
* Called when the activity is first created.
*/
int count=0;
Button btnSubmitBarcode;
EditText txtPhelebotomistid, txtBarcode;
RadioGroup rad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
invokeWS();
setContentView(R.layout.main2);
}
public void invokeWS(){
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.10.8:8080/useraccount/barcode/periodcount",new AsyncHttpResponseHandler() {
// When the response returned by REST has Http response code '200'
@Override
public void onSuccess(String response) {
addRadioButtons(response);
}
// When the response returned by REST has Http response code other than '200'
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
PeriodChange m=new PeriodChange();
// When Http response code is '404'
if(statusCode == 404){
//Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
m.showMessage("not found","Requested resource not found");
}
// When Http response code is '500'
else if(statusCode == 500){
//Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
m.showMessage("wrong","Something went wrong at server end");
}
// When Http response code other than 404, 500
else{
//Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
m.showMessage("error","Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]");
}
}
});
}
public void showMessage(String title, String message) {
Builder builder = new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void addRadioButtons(String response){
int x=Integer.parseInt(response);
RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);
RadioGroup.LayoutParams rprms;
// System.out.println("count1---X---"+x);
Log.d("count","count");
for(int i=0;i<x;i++){
RadioButton radioButton = new RadioButton(this);
radioButton.setText("period"+i);
radioButton.setId(i);
rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rgp.addView(radioButton, rprms);
}
}
}
以下是XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/radiogroup"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
以下是Logcat
12-11 09:59:18.860: W/KeyCharacterMap(275): No keyboard for id 0
12-11 09:59:18.860: W/KeyCharacterMap(275): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-11 09:59:28.559: D/dalvikvm(275): GC_FOR_MALLOC freed 6569 objects / 321184 bytes in 76ms
12-11 09:59:39.879: W/System.err(275): java.net.SocketTimeoutException
12-11 09:59:39.889: W/System.err(275): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564)
12-11 09:59:39.889: W/System.err(275): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
12-11 09:59:39.899: W/System.err(275): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
12-11 09:59:39.899: W/System.err(275): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
12-11 09:59:39.899: W/System.err(275): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
12-11 09:59:39.909: W/System.err(275): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
12-11 09:59:39.909: W/System.err(275): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
12-11 09:59:39.909: W/System.err(275): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
12-11 09:59:39.909: W/System.err(275): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
12-11 09:59:39.909: W/System.err(275): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
12-11 09:59:39.919: W/System.err(275): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
12-11 09:59:39.919: W/System.err(275): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
12-11 09:59:39.919: W/System.err(275): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-11 09:59:39.919: W/System.err(275): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-11 09:59:39.919: W/System.err(275): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
12-11 09:59:39.919: W/System.err(275): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
12-11 09:59:39.919: W/System.err(275): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
12-11 09:59:39.929: W/System.err(275): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
12-11 09:59:39.929: W/System.err(275): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-11 09:59:39.929: W/System.err(275): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-11 09:59:39.929: W/System.err(275): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-11 09:59:39.929: W/System.err(275): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-11 09:59:39.929: W/System.err(275): at java.lang.Thread.run(Thread.java:1096)
12-11 09:59:39.929: W/System.err(275): java.net.SocketTimeoutException
12-11 09:59:39.929: W/System.err(275): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564)
12-11 09:59:39.929: W/System.err(275): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
12-11 09:59:39.939: W/System.err(275): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
12-11 09:59:39.949: W/System.err(275): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
12-11 09:59:39.949: W/System.err(275): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
12-11 09:59:39.949: W/System.err(275): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-11 09:59:39.949: W/System.err(275): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-11 09:59:39.949: W/System.err(275): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
12-11 09:59:39.949: W/System.err(275): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
12-11 09:59:39.949: W/System.err(275): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
12-11 09:59:39.949: W/System.err(275): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
12-11 09:59:39.949: W/System.err(275): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-11 09:59:39.949: W/System.err(275): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-11 09:59:39.949: W/System.err(275): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-11 09:59:39.959: W/System.err(275): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-11 09:59:39.959: W/System.err(275): at java.lang.Thread.run(Thread.java:1096)
12-11 09:59:39.959: D/AndroidRuntime(275): Shutting down VM
12-11 09:59:39.959: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-11 09:59:39.979: E/AndroidRuntime(275): FATAL EXCEPTION: main
12-11 09:59:39.979: E/AndroidRuntime(275): java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-11 09:59:39.979: E/AndroidRuntime(275): at android.app.Activity.getSystemService(Activity.java:3526)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743)
12-11 09:59:39.979: E/AndroidRuntime(275): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.mkyong.android.PeriodChange.showMessage(PeriodChange.java:154)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.mkyong.android.PeriodChange$2.onFailure(PeriodChange.java:146)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.loopj.android.http.AsyncHttpResponseHandler.onFailure(AsyncHttpResponseHandler.java:301)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.loopj.android.http.AsyncHttpResponseHandler.onFailure(AsyncHttpResponseHandler.java:315)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:373)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:135)
12-11 09:59:39.979: E/AndroidRuntime(275): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 09:59:39.979: E/AndroidRuntime(275): at android.os.Looper.loop(Looper.java:123)
12-11 09:59:39.979: E/AndroidRuntime(275): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-11 09:59:39.979: E/AndroidRuntime(275): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 09:59:39.979: E/AndroidRuntime(275): at java.lang.reflect.Method.invoke(Method.java:521)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-11 09:59:39.979: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-11 09:59:39.979: E/AndroidRuntime(275): at dalvik.system.NativeStart.main(Native Method)
12-11 10:04:40.040: I/Process(275): Sending signal. PID: 275 SIG: 9
这是我从webservice
调用的方法@GET
// Path: http://localhost/<appln-folder-name>/login/dologin
@Path("/periodcount")
// Produces JSON as response
@Produces(MediaType.APPLICATION_JSON)
public String getNumberOfPeriods(){
String response = "";
try
{
DBConnection db=new DBConnection();
System.out.println("s1");
int studyID=DBConnection.getStudyId();
System.out.println(studyID);
//System.out.println("s2");
response = String.valueOf(DBConnection.getPeriods(studyID));
//System.out.println(studieslist);
//System.out.println("s3");
//Gson gson = new Gson();
//System.out.println("s4");
//System.out.println(gson.toJson(studieslist));
//System.out.println("s5");
//response = gson.toJson(studieslist);
//System.out.println("s6");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Exception Error"); //Console
}
return response;
}
这是getStudyId()方法
public static int getStudyId() throws Exception {
boolean isUserAvailable = false;
Connection dbConn = null;
try {
try {
dbConn = DBConnection.createConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(employeeId);
Statement stmt = dbConn.createStatement();
System.out.println(employeeId);
String query = "SELECT * FROM active_study_details WHERE emp_id = '" + employeeId+"'";
ResultSet rs = stmt.executeQuery(query);
System.out.println(employeeId);
while (rs.next()) {
System.out.println("eid.....dddd......"+rs.getInt("study_id"));
studyID=rs.getInt("study_id");
System.out.println(studyID);
}
} catch (SQLException sqle) {
throw sqle;
} catch (Exception e) {
// TODO Auto-generated catch block
if (dbConn != null) {
dbConn.close();
}
throw e;
} finally {
if (dbConn != null) {
dbConn.close();
}
}
return studyID;
}
这是getPeriods(int studyID)方法
public static int getPeriods(int studyID) throws Exception {
boolean isUserAvailable = false;
int count=0;
Connection dbConn = null;
try {
try {
dbConn = DBConnection.createConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(studyID);
Statement stmt = dbConn.createStatement();
System.out.println(studyID);
String query = "SELECT * FROM study_period WHERE study_id = '" + studyID+"'";
//String query = "SELECT * FROM active_study_details WHERE emp_id = '" + employeeId+"'";
ResultSet rs = stmt.executeQuery(query);
System.out.println(studyID);
while (rs.next()) {
//System.out.println("eid.....dddd......"+rs.getInt("study_id"));
count++;
System.out.println(count);
//System.out.println(studyID);
}
} catch (SQLException sqle) {
throw sqle;
} catch (Exception e) {
// TODO Auto-generated catch block
if (dbConn != null) {
dbConn.close();
}
throw e;
} finally {
if (dbConn != null) {
dbConn.close();
}
}
System.out.println(count);
return count;
}
提前致谢,
答案 0 :(得分:0)
尝试以下代码: -
for(int i=1;i<=x;i++){
RadioButton radioButton = new RadioButton(this);
radioButton.setText("period"+i);
radioButton.setId(i*2 + 1);
rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rgp.addView(radioButton, rprms);
}
<强>更新: - &GT;
在你的oncreated而不是你的方法中添加以下行: -
RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);