2013-06-19 16 views
1

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

如果我们点击它具有获取ID的按钮,它在点击该按钮,现在它的节目后发送id.But“你的应用程序已经停止”

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的showning。

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> 
+0

请提供祈祷按钮 – Dimmerg

+0

不是无神论,它像按钮名称XML我给@ MarcoForberg –

+0

@Dimmerg,我提到我的布局也在这里,你可以检查它 –

回答

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(); 
    } 
+0

你发布你的代码我会尝试 –

+0

好的,我已经发布。 – Dimmerg

+0

Dude实际上在这个布局“R.layout.activity_list_item”我没有它的acitivity_row中的祈祷按钮,这就是为什么我喜欢android:onClick =“祈祷”是这可能把另一个布局在创建功能 –

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); 
+0

它可能在那布局是没有那个按钮,我们可以把里面的oncreate功能 –

+0

请检查它,如果没有得到那么我认为需要使用LayoutInflater,并会尝试 –

+0

其不工作@Ankit Makwana –

相关问题