1

在我的列表视图中,如果我单击它必须获取列表视图 ID 的按钮,我已经放置了一个按钮,但是在单击该按钮后应用程序已停止。

如果我们点击按钮,它已经获取了 id,它必须发送 id。但是现在点击按钮后,它会显示“您的应用程序已停止”

   public class MainActivity extends Activity implements FetchDataListener,OnClickListener
{
    private static final int ACTIVITY_CREATE=0;
    private ProgressDialog dialog;
    ListView lv;
    ListView lv1;
    private List<Application> items;
    private Button btnGetSelected;
    private Button praycount;
    public int pct;
    private String stringVal;
    private TextView value;
    private int prayers;




    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_list_item);  




    lv1 =(ListView)findViewById(R.id.list); 
     lv =(ListView)findViewById(R.id.list);
     btnGetSelected = (Button) findViewById(R.id.btnget);
        btnGetSelected.setOnClickListener(this);


        initView();
    }






    private void initView()
    {
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");
        String url = "http://www.ginfy.com/api/v1/posts.json";
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);

    }   





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
         MenuInflater mi = getMenuInflater();
            mi.inflate(R.menu.activity_main, menu); 
        return true;

    }

     @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {

                createProject();

            return super.onMenuItemSelected(featureId, item);
     }

     private void createProject() {
            Intent i = new Intent(this, AddPrayerActivity.class);
            startActivityForResult(i, ACTIVITY_CREATE);   
        }

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            initView();
        }

    @Override
    public void onFetchComplete(List<Application> data)
    {
        this.items = data;
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // create new adapter
        ApplicationAdapter adapter = new ApplicationAdapter(this, data);
        // set the adapter to list
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                CheckBox chk = (CheckBox) view.findViewById(R.id.checkbox);
                Application bean = items.get(position);
                if (bean.isSelected()) {
                    bean.setSelected(false);
                    chk.setChecked(false);
                } else {
                    bean.setSelected(true);
                    chk.setChecked(true);
                }

            }
        });
    }

    // Toast is here...
        private void showToast(String msg) {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }

    @Override
    public void onFetchFailure(String msg)
    {
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // show failure message
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }



    @Override
    public void onClick(View v) {


        StringBuffer sb = new StringBuffer();

        // Retrive Data from list
        for (Application bean : items) {

            if (bean.isSelected()) {
                sb.append("Title:");
                sb.append(Html.fromHtml(bean.getTitle()));
                sb.append(",Content:");
                sb.append(Html.fromHtml(bean.getContent()));


                sb.append("\n");

            }


        }

        showAlertView(sb.toString().trim());

    }

    public void onPrayComplete(List<Application> data)
    {
        this.items = data;
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // create new adapter
        ApplicationAdapter adapter = new ApplicationAdapter(this, data);
        // set the adapter to list
        lv1.setAdapter(adapter);
        lv1 .setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {


                Button pct = (Button) view.findViewById(R.id.pray);
                //Application bean1 = items.get(position);
                Object o = items.get(position);
                Application obj_itemDetails = (Application)o;
                Integer prayers = obj_itemDetails.getId(); 

            }
        });
    }


    public void pray(View v) {

        //showAlertView(sb.toString().trim());


        praydata(Integer.parseInt("prayers"));
    }

    public void praydata(int i) {
        // Create a new HttpClient and Post Header.

        String url= "http://www.ginfy.com/api/v1/posts/"+i+".json";
        dialog = ProgressDialog.show(this, "", "Loading...");
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);

    }

这是显示的 logcat。

06-19 11:16:31.645: E/AndroidRuntime(3481): FATAL EXCEPTION: main
06-19 11:16:31.645: E/AndroidRuntime(3481): java.lang.IllegalStateException: Could not find a method pray(View) in the activity class com.example.jsonandroid.MainActivity for onClick handler on view class android.widget.Button with id 'pray'
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.view.View$1.onClick(View.java:3584)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.view.View.performClick(View.java:4202)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.view.View$PerformClick.run(View.java:17340)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.os.Handler.handleCallback(Handler.java:725)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.os.Looper.loop(Looper.java:137)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.app.ActivityThread.main(ActivityThread.java:5039)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at dalvik.system.NativeStart.main(Native Method)
06-19 11:16:31.645: E/AndroidRuntime(3481): Caused by: java.lang.NoSuchMethodException: pray [class android.view.View]
06-19 11:16:31.645: E/AndroidRuntime(3481):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at java.lang.Class.getMethod(Class.java:915)
06-19 11:16:31.645: E/AndroidRuntime(3481):     at android.view.View$1.onClick(View.java:3577)
06-19 11:16:31.645: E/AndroidRuntime(3481):     ... 11 more

这是我的祈祷按钮 xml 文件 activity_row.xml 的布局

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

    <TextView 

    android:id="@+id/text2" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"

    android:layout_margin="5dp"

    android:textColor="@android:color/black"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/text1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text2"
    android:layout_margin="5dp"
    android:textColor="@android:color/black"
    android:textSize="15sp"
    android:textStyle="bold" />
<TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/checkbox"

        android:layout_marginRight="20dip" />
<Button
        android:id="@+id/pray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/count"
        android:layout_marginTop="30dip"
       android:layout_toLeftOf="@+id/checkbox"
       android:text="Pray"
        android:background="@drawable/pray"
        android:onClick="pray"
        android:clickable="true" />




    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip"
        android:background="@drawable/chk"
        android:button="@null"
        android:focusable="false"
        android:focusableInTouchMode="false" />


</RelativeLayout>
4

2 回答 2

0

我认为问题应该在这里

在 Oncreate

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

        LinearLayout itemLayout = (LinearLayout) layoutInflater.inflate(
                R.layout.yourlayout, this);

         Button pray= (Button) itemLayout
                .findViewById(R.id.pray);
于 2013-06-19T12:32:17.263 回答
0

在代码中使用 setOnClickListener 设置监听器更可靠。试试这个,别担心)

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_list_item);  




    lv1 =(ListView)findViewById(R.id.list); 
     lv =(ListView)findViewById(R.id.list);
     btnGetSelected = (Button) findViewById(R.id.btnget);
        btnGetSelected.setOnClickListener(this);

     Button prayBtn = (Button)findViewById(R.id.pray)
     prayBtn .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pray(v);
            }
        });


        initView();
    }
于 2013-06-19T12:20:50.203 回答