按钮单击功能在Android中抛出错误

时间:2014-04-03 13:12:41

标签: android button click

单击“管理器”按钮时,我遇到了问题。还附有Logcat信息。

我的XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button android:id="@+id/manager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    />
    <Button android:id="@+id/namedetail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    />
   <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
 /LinearLayout>

当我单击“管理器”按钮时,我收到错误消息。 Logcat也附上了。

Java程序:

public class NextActivity extends Activity {
    private ArrayList<String> results = new ArrayList<String>();
    private String tableName = DBHelper.tableName;
    private SQLiteDatabase newDB;
    // List view
    private ListView lv;
    String Name ;
    String[] FirstName = new String[2];
    // Listview Adapter
    ArrayAdapter<String> adapter;
    public static String TLanid,MLanid;
    // Search EditText
    Button manager;
    Button  namedetail;

    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next_main);
        // Listview Data
        final String resultempty[] = {};

        lv = (ListView) findViewById(R.id.list_view);
        //lv.setDividerHeight(10);
        manager = (Button) findViewById(R.id.manager);  
        manager.setTextColor(Color.GREEN);
        manager.setTextSize(10);
        manager.setTypeface(null,Typeface.BOLD);
        namedetail = (Button) findViewById(R.id.namedetail);
        namedetail.setTextColor(Color.GREEN);

        namedetail.setTextSize(25);
        Name = getIntent().getExtras().getString("Name");
        FirstName = Name.split(" ");
        FetchQueryDatabase(FirstName[1],FirstName[0]);

        // Adding items to listview
        adapter = new ArrayAdapter(this, R.layout.list_item, R.id.product_name, results);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {
                    @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                        int itemPosition     = arg2;
                        // ListView Clicked item value
                        String  itemValue    = (String) lv.getItemAtPosition(arg2);
                        itemValue = itemValue.trim();
                        FirstName = itemValue.split(" ");
                         Toast.makeText(getApplicationContext(),
                               "Name :"+ FirstName[0] + FirstName[1] , Toast.LENGTH_LONG)
                              .show();
                         FetchQueryDatabase(FirstName[1],FirstName[0]);
                        //FetchQueryDatabase(itemValue);
                        setadapter();
            }
        });        

        manager.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String itemValue = (String) manager.getText();
                 Toast.makeText(getApplicationContext(),
                       "Name :"+ itemValue , Toast.LENGTH_LONG)
                      .show();
            }
        }); 

    }

    @SuppressWarnings("unchecked")
    public void setadapter()
    {
     adapter = new ArrayAdapter(getApplicationContext(), R.layout.list_item, R.id.product_name, results);
     lv.setAdapter(adapter);
    }

}

Logcat信息:

04-03 18:30:44.678: E/AndroidRuntime(204): Uncaught handler: thread main exiting due to uncaught exception
04-03 18:30:44.708: E/AndroidRuntime(204): java.lang.ClassCastException: android.text.SpannableStringBuilder
04-03 18:30:44.708: E/AndroidRuntime(204):  at com.androidhive.androidlistviewwithsearch.NextActivity$1.onClick(NextActivity.java:87)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.View.performClick(View.java:2364)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.View.onTouchEvent(View.java:4179)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.View.dispatchTouchEvent(View.java:3709)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-03 18:30:44.708: E/AndroidRuntime(204):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-03 18:30:44.708: E/AndroidRuntime(204):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-03 18:30:44.708: E/AndroidRuntime(204):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.os.Looper.loop(Looper.java:123)
04-03 18:30:44.708: E/AndroidRuntime(204):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-03 18:30:44.708: E/AndroidRuntime(204):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 18:30:44.708: E/AndroidRuntime(204):  at java.lang.reflect.Method.invoke(Method.java:521)
04-03 18:30:44.708: E/AndroidRuntime(204):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-03 18:30:44.708: E/AndroidRuntime(204):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

2 个答案:

答案 0 :(得分:1)

你会把它变成这样......

 String itemValue = (String) manager.getText();

这样改变......下面

 String itemValue =  manager.getText().toString();

答案 1 :(得分:0)

也许你必须以这种方式使用toString()方法:

String itemValue = (String) manager.getText().toString();