0

我一直在尝试以编程方式在 android 的弹出窗口中将单选按钮添加到单选组,但没有成功。当我尝试将新创建的 radioButton 添加到 radioGroup 时,应用程序关闭。任何建议将不胜感激!这是代码:

 {
    RadioGroup rg = (RadioGroup) findViewById(R.id.food_group);
             for (int i = 0; i < matches.size(); i++) {
                 Log.v("AddFood", "i " + i);
             RadioButton radiobutton = new RadioButton(this);
           radiobutton.setText(matches.get(i));
            radiobutton.setId(i);
            Log.v("AddFood", "here1");
            rg.addView(radiobutton);
            Log.v("AddFood", "here2");

        }
        LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
       View popupView = layoutInflater.inflate(R.layout.popup_database_input, null); 
    popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,  
                     LayoutParams.WRAP_CONTENT);  

        popupWindow.setFocusable(true);




        Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
        btnDismiss.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }});

        Button btnOK = (Button)popupView.findViewById(R.id.accept);
        btnOK.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }});
    }


    popupWindow.showAtLocation(arg0, Gravity.CENTER, 0, 0);

这是弹出窗口的xml:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:layout_weight="0.67"
        android:background="@android:color/darker_gray"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="258dp"
                android:layout_height="59dp"
                android:text="@string/found_match"
                android:textColor="@android:color/black" />

            <RadioGroup
                android:id="@+id/food_group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >


            </RadioGroup>



            <Button
                android:id="@+id/dismiss"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/Cancel" />

            <Button
                android:id="@+id/accept"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/ok" />
        </LinearLayout>
    </LinearLayout>

    </LinearLayout>

这是日志猫:

04-23 15:04:15.142: D/gralloc_goldfish(2307): Emulator without GPU emulation detected.
04-23 15:04:27.802: D/Home(2307): switching activities...
04-23 15:04:28.912: D/dalvikvm(2307): GC_CONCURRENT freed 76K, 7% free 2711K/2908K,      paused 71ms+10ms, total 225ms
04-23 15:04:28.912: D/dalvikvm(2307): WAIT_FOR_CONCURRENT_GC blocked 137ms
04-23 15:04:28.922: I/dalvikvm-heap(2307): Grow heap (frag case) to 3.370MB for 635812-byte allocation
04-23 15:04:29.052: D/dalvikvm(2307): GC_FOR_ALLOC freed 4K, 6% free 3328K/3532K, paused 126ms, total 126ms
04-23 15:04:29.333: V/addSugar(2307): creating
04-23 15:04:29.952: I/Choreographer(2307): Skipped 89 frames!  The application may be doing too much work on its main thread.
04-23 15:04:32.002: V/addSugar(2307): searchFoodButton clicked. food is:
04-23 15:04:32.102: W/System.err(2307): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=ec2-50-17-119-232-compute-1.amazonaws.com/glucose/find_food/
04-23 15:04:32.111: V/AddFood(2307): i 0
04-23 15:04:32.222: V/AddFood(2307): here1
04-23 15:04:32.222: D/AndroidRuntime(2307): Shutting down VM
04-23 15:04:32.232: W/dalvikvm(2307): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-23 15:04:32.271: E/AndroidRuntime(2307): FATAL EXCEPTION: main
04-23 15:04:32.271: E/AndroidRuntime(2307): java.lang.IllegalStateException: Could not execute method of the activity
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.view.View$1.onClick(View.java:3599)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.view.View.performClick(View.java:4204)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.view.View$PerformClick.run(View.java:17355)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.os.Handler.handleCallback(Handler.java:725)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.os.Looper.loop(Looper.java:137)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at java.lang.reflect.Method.invoke(Method.java:511)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at dalvik.system.NativeStart.main(Native Method)
04-23 15:04:32.271: E/AndroidRuntime(2307): Caused by: java.lang.reflect.InvocationTargetException
04-23 15:04:32.271: E/AndroidRuntime(2307):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at java.lang.reflect.Method.invoke(Method.java:511)
04-23 15:04:32.271: E/AndroidRuntime(2307):     at android.view.View$1.onClick(View.java:3594)
04-23 15:04:32.271: E/AndroidRuntime(2307):     ... 11 more
04-23 15:04:32.271: E/AndroidRuntime(2307): Caused by: java.lang.NullPointerException
04-23 15:04:32.271: E/AndroidRuntime(2307):     at        com.insulinapp.AddSugar.searchFoodHandler(AddSugar.java:99)
04-23 15:04:32.271: E/AndroidRuntime(2307):     ... 14 more
04-23 15:04:34.592: I/Process(2307): Sending signal. PID: 2307 SIG: 9
4

0 回答 0