Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > เอาโค๊ด Eclipe มาใส่ในAndroid studio แล้ว error ว่า "method gettext must be called from the ui thread" แก้ไขยังไงครับ



 

เอาโค๊ด Eclipe มาใส่ในAndroid studio แล้ว error ว่า "method gettext must be called from the ui thread" แก้ไขยังไงครับ

 



Topic : 123749



โพสกระทู้ ( 82 )
บทความ ( 0 )



สถานะออฟไลน์




ผมใช้โค้ดเก่าใน eclip มาแปลงใช้ใน Android studio ครับ คือผมจะเชื่อมต่อกับ mysql
แต่ปัญหาคือ มันขึ้น error ตรงแถบสีแดง(เลื่อนไปดูในโค้ดนะครับ)ครับ มันขึ้นว่า "method gettext must be called from the ui thread"
รบกวนช่วยด้วยครับ


EditActivity.java
package com.example.rattapongt.selerdbsystem; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class EditActivity extends Activity implements OnClickListener { EditText etId, etTitle, etDesc, etPrice, etSaler, etDate; Button btnUpdate, btnDelete; int pro_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit); etId = (EditText)findViewById(R.id.etId); etTitle = (EditText)findViewById(R.id.etTitle); etDesc = (EditText)findViewById(R.id.etDesc); etPrice = (EditText)findViewById(R.id.etPrice); etSaler = (EditText)findViewById(R.id.etSaler); etDate = (EditText)findViewById(R.id.etDate); btnUpdate = (Button)findViewById(R.id.btnUpdate); btnDelete = (Button)findViewById(R.id.btnDelete); btnUpdate.setOnClickListener(this); btnDelete.setOnClickListener(this); Bundle extras = getIntent().getExtras(); if(extras == null){ return; } pro_id = extras.getInt("pro_id"); etId.setText("" + pro_id); ReadData task1 = new ReadData(); task1.execute(new String[]{"http://10.0.2.2:8012/home_onlinesale/?format=json&id=" + pro_id}); } Product product = new Product(); private class ReadData extends AsyncTask<String, Void, Boolean>{ private ProgressDialog dialog = new ProgressDialog(EditActivity.this); private String error; InputStream is1; String text = ""; @Override protected void onPreExecute() { dialog.setMessage("Reading Data..."); dialog.show(); } @Override protected Boolean doInBackground(String... urls) { for(String url: urls){ try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); HttpResponse response = client.execute(post); is1 = response.getEntity().getContent(); } catch (ClientProtocolException e) { error = "ClientProtocolException: " + e.getMessage(); return false; } catch (IOException e) { error = "ClientProtocolException: " + e.getMessage(); } } BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(is1 ,"iso-8859-1"), 8); String line = null; while ((line = reader.readLine()) != null) { text += line + "\n"; } is1.close(); } catch (UnsupportedEncodingException e) { error = "Unsupport Encoding: " + e.getMessage(); } catch (IOException e) { error = "Error IO: " + e.getMessage(); } try { JSONArray jArray = new JSONArray(text); for(int i=0; i<jArray.length(); i++){ JSONObject json = jArray.getJSONObject(i); product.setId(json.getInt("pro_id")); product.setTitle(json.getString("pro_title")); product.setDesc(json.getString("pro_desc")); product.setPrice(json.getDouble("pro_price")); product.setSaler(json.getString("pro_saler")); product.setDate(json.getString("pro_date")); } } catch (JSONException e) { error = "Error Convert to JSON or Error JSON Format: " + e.getMessage(); } return true; } @Override protected void onPostExecute(Boolean result) { if(dialog.isShowing()){ dialog.dismiss(); } if(result == false){ Toast.makeText(EditActivity.this, error, Toast.LENGTH_LONG).show(); } else{ etTitle.setText(product.getTitle()); etDesc.setText(product.getDesc()); etPrice.setText("" + product.getPrice()); etSaler.setText(product.getSaler()); etDate.setText(product.getDate()); } } }//End of private class ReadData String updateTrigger = ""; @Override public void onClick(View sender) { if(sender.getId() == R.id.btnUpdate){ UpdateData taskUpdate = new UpdateData(); updateTrigger = "Update"; taskUpdate.execute(new String[]{"http://10.0.2.2:8012/home_onlinesale/edit.php?id="+pro_id}); Intent in = new Intent(this, MainActivity.class); startActivity(in); } else if(sender.getId() == R.id.btnDelete){ Builder msgBox = new AlertDialog.Builder(this); msgBox.setTitle("Delete Confirmation"); msgBox.setMessage("Are you sure to delete it?"); msgBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); msgBox.setPositiveButton("Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { UpdateData taskUpdate = new UpdateData(); updateTrigger = "Delete"; taskUpdate.execute(new String[]{"http://10.0.2.2:8012/home_onlinesale/edit.php?id="+pro_id}); Intent in = new Intent(EditActivity.this, MainActivity.class); startActivity(in); } }); msgBox.show(); } } private class UpdateData extends AsyncTask<String, Void, Boolean>{ private ProgressDialog dialog = new ProgressDialog(EditActivity.this); private String error; String text = ""; @Override protected void onPreExecute() { dialog.setMessage("Editting Data..."); dialog.show(); } InputStream is1; @Override protected Boolean doInBackground(String... urls) { setOn for(String url: urls){ try { ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("btnSubmit", updateTrigger)); pairs.add(new BasicNameValuePair("txtProTitle", etTitle.getText().toString())); pairs.add(new BasicNameValuePair("txtProDesc", etDesc.getText().toString())); pairs.add(new BasicNameValuePair("txtProPrice", etPrice.getText().toString())); pairs.add(new BasicNameValuePair("txtProSaler", etSaler.getText().toString())); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(post); is1 = response.getEntity().getContent(); } catch (ClientProtocolException e) { error = "ClientProtocolException: " + e.getMessage(); return false; } catch (IOException e) { error = "ClientProtocolException: " + e.getMessage(); } } return true; } @Override protected void onPostExecute(Boolean result) { if(dialog.isShowing()){ dialog.dismiss(); } if(result == false){ Toast.makeText(EditActivity.this, error, Toast.LENGTH_LONG).show(); } else{ if(is1 == null){ Toast.makeText(EditActivity.this, "Sending Wrong Parameters", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(EditActivity.this, "Edit Success", Toast.LENGTH_LONG).show(); } } } } }




Tag : Mobile, MySQL, Android, JAVA, Mobile









ประวัติการแก้ไข
2016-07-08 10:37:11
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-07-08 10:34:26 By : rattapongza View : 1264 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

สงสัยมันหาพวก Widget ที่เป็น etTitle ไม่เจอนะครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-07-08 11:14:00 By : mr.win
 


 

No. 2



โพสกระทู้ ( 82 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2016-07-08 11:14:00
รายละเอียดของการตอบ ::
มันหาเจอนะครับ ตอนผมพิมพ์ .(ดอท) มันก็มีชื่อเด้งมาให้เลือกเลย
คือตัวแดงๆที่ผมใส่ให้ดู จริงๆมันไม่ได้ตัวแดงนะครับ มันเป็นขีดเส้นใต้สีแดงครับ
"method gettext must be called from the ui thread"


อันนี้เป็นโค๊ด xml ครับ

activity_edit.xml]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EditActivity" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product ID"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"

        />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Desc"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etDesc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etPrice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Saler"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etSaler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Post Date"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"

        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnUpdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Update" />

        <Button
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete" />

    </LinearLayout>

</LinearLayout>




แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-07-08 13:06:53 By : rattapongza
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : เอาโค๊ด Eclipe มาใส่ในAndroid studio แล้ว error ว่า "method gettext must be called from the ui thread" แก้ไขยังไงครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่