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 > ผมเอาบทความ Android ListView and Checkbox มาปรับใช้กับงานของผม แต่มันเกิด error NullPointerException อยากขอคำแนะนำหน่อยว่าผมทำอะไรตรงไหนพลาดไป



 

ผมเอาบทความ Android ListView and Checkbox มาปรับใช้กับงานของผม แต่มันเกิด error NullPointerException อยากขอคำแนะนำหน่อยว่าผมทำอะไรตรงไหนพลาดไป

 



Topic : 117137



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



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




บทความที่เอามาปรับใช้กับงาน https://www.thaicreate.com/mobile/android-listview-checkbox.html ตามลิ้งค์ พอรันแอพก็เกิด error NullPointerException ตรงบรรทัดที่่ CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.promotionTypeChk);

Code Java
public class PromotionTypeList extends Activity {

    private static final String TAG_PROMOTION_Type = "promotion_type";
    private static final String TAG_PROMOTION_Type_ID = "proTypeID";
    private static final String TAG_PROMOTION_Type_NAME = "proTypeName";

    JSONArray PromoTypeArray = null;

    public String getJson= null;

    ListView listView;
    SimpleAdapter adapter;

    Button btnGetType;

    ArrayList<HashMap<String, String>> promotionTypeList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.promotion_type_list);

        ActionBar actionBar = getActionBar();
        actionBar.setTitle("Promotion Type");

        listView = (ListView) findViewById(R.id.listView4);

        getJson = getIntent().getStringExtra("PromotionType");
        Log.d("GetJson", getJson);

        JSONObject jsonObject = null;

        try {
            PromoTypeArray = jsonObject.getJSONArray(TAG_PROMOTION_Type);

            if (PromoTypeArray.length() == 0) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle("Caution!");
                dialog.setIcon(R.drawable.ic_action_error);
                dialog.setCancelable(false);
                dialog.setMessage("No Promotion Type Data.");
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(getApplicationContext(), Find_Promotion.class);
                        startActivity(i);
                        finish();
                    }
                });
                dialog.show();
            } else {
                Log.d("SIZE ARRAY", String.valueOf(PromoTypeArray.length()));
                for (int i = 0; i < PromoTypeArray.length(); i++) {
                    JSONObject object = PromoTypeArray.getJSONObject(i);

                    String proType_id = object.getString(TAG_PROMOTION_Type_ID);
                    String proType_name = object.getString(TAG_PROMOTION_Type_NAME);

                    Log.d("proType_id", proType_id);
                    Log.d("proType_name", proType_name);

                    HashMap<String, String> proType = new HashMap<String, String>();

                    proType.put(TAG_PROMOTION_Type_ID, proType_id);
                    proType.put(TAG_PROMOTION_Type_NAME, proType_name);

                    promotionTypeList.add(proType);
                }

            }
        } catch (JSONException e) {
            Log.d("JSON PARSER", "Parsing Error");
        }

        listView.setAdapter(new CountryAdapter(this));

        btnGetType = (Button) findViewById(R.id.protypeToFindPro);
        btnGetType.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int countArray = listView.getAdapter().getCount();
                Log.d("count", String.valueOf(countArray));

                for (int j = 0; j < countArray; j++) {
                    RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(j);
                    
                    if(checkbox.isChecked())
                    {
                        Log.d("Item "+String.valueOf(j), checkbox.getTag().toString());

                        Toast.makeText(PromotionTypeList.this,checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }

    public class CountryAdapter extends BaseAdapter {
        private Context context;

        public CountryAdapter(Context c) {
            //super( c, R.layout.activity_column, R.id.rowTextView, );
            // TODO Auto-generated method stub
            context = c;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return promotionTypeList.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.promotion_type_item, null);

            }

            // Type ID
            TextView txtID = (TextView) convertView.findViewById(R.id.promotionTypeId);
            txtID.setText(promotionTypeList.get(position).get("proTypeID") + ".");

            // Type Name
            TextView txtCode = (TextView) convertView.findViewById(R.id.promotionTypeName);
            txtCode.setText(promotionTypeList.get(position).get("proTypeName"));

            // Type Check
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.promotionTypeChk);
            Chk.setTag(promotionTypeList.get(position).get("proTypeID"));

            return convertView;

        }
    }

    public void onBackPressed(){
        Intent intent = new Intent(getApplicationContext(), Find_Promotion.class);
        startActivity(intent);
    }
}


list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="   OK   "
        android:id="@+id/protypeToFindPro"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView4"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="25dp"
        android:background="#ffffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You can select more than one"
        android:id="@+id/textView12"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textSize="20dp"
        android:layout_marginLeft="5dp"/>
</RelativeLayout>


Item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/promotionTypeId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="0dp"
        />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/promotionTypeChk"
        android:textColor="#ffffffff"
        android:textSize="20dp"
        android:padding="15dp"
        android:layout_marginLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Free"
        android:id="@+id/promotionTypeName"
        android:textSize="20dp"
        android:padding="15dp"
        android:layout_marginLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>


Logcat
06-13 18:45:29.881 9957-9957/bkk.promotion D/count﹕ 9 06-13 18:45:29.881 9957-9957/bkk.promotion D/Item 0﹕ 1 06-13 18:45:29.881 9957-9957/bkk.promotion D/AndroidRuntime﹕ Shutting down VM 06-13 18:45:29.881 9957-9957/bkk.promotion W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415b9bc0) 06-13 18:45:29.881 9957-9957/bkk.promotion E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: bkk.promotion, PID: 9957 java.lang.NullPointerException at bkk.promotion.PromotionTypeList$2.onClick(PromotionTypeList.java:142) at android.view.View.performClick(View.java:4446) at android.view.View$PerformClick.run(View.java:18437) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5372) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) at dalvik.system.NativeStart.main(Native Method)




Tag : Mobile, Device (Mobile), Android, Mobile









ประวัติการแก้ไข
2015-06-13 18:51:43
2015-06-13 18:55:59
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-06-13 18:28:59 By : funrun View : 1285 Reply : 2
 

 

No. 1



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



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


ตอนนี้รับค่าได้แล้วครับ แต่เจอปัญหาคือ ถ้า listview มีข้อมูลที่จะโชว์เกินหน้าจอจะทำให้ error เช่น มีข้อมูลที่จะโชว์ 10 ตัว แต่ listview โชว์ได้แค่ 7 ก็เต็มจออีก3ตัวต้องเลื่อนลงไปถึงเห็น ถ้าเกิดเห็นแบบนี้พอกดปุ่มจะรับค่าไม่ได้ และเกิด error ครับ แต่ถ้ามีข้อมูลที่จะโชว์ 5 ตัว แต่ listview โชว์ได้แค่ 7 แบบนี้จะรับค่าได้หมด เหมือนว่า มีปัญหากับการใช้ลูปอะครับ ยังไงช่วยแนะนำทีว่าควรจะแก้ไขยังไงต่อไป เพราะผมมีอยู่1หน้าที่ต้องโชว์ข้อมูล20กว่าตัว ถ้าเป็นแบบนี้จะหน้านี้เป็น bug แล้วใช้งานหน้านี้ไม่ได้






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-13 21:46:14 By : funrun
 


 

No. 2



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



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


คุณ funrun
แล้ว แบบนี้ แก้ปัญหา ยังไง ครับ
ขอบคุณครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-08-05 18:00:44 By : brambroza
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ผมเอาบทความ Android ListView and Checkbox มาปรับใช้กับงานของผม แต่มันเกิด error NullPointerException อยากขอคำแนะนำหน่อยว่าผมทำอะไรตรงไหนพลาดไป
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 02
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 อัตราราคา คลิกที่นี่