0

我是初学者,我需要解决这个问题。

09-29 11:53:50.539: E/AndroidRuntime(658): FATAL EXCEPTION: main
09-29 11:53:50.539: E/AndroidRuntime(658): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
09-29 11:53:50.539: E/AndroidRuntime(658):  at com.example.listactivity.MainActivity$MyCustomAdapter$1.onClick(MainActivity.java:91)
09-29 11:53:50.539: E/AndroidRuntime(658):  at android.view.View.performClick(View.java:3511)
09-29 11:53:50.539: E/AndroidRuntime(658):  at android.view.View$PerformClick.run(View.java:14105)
09-29 11:53:50.539: E/AndroidRuntime(658):  at android.os.Handler.handleCallback(Handler.java:605)
09-29 11:53:50.539: E/AndroidRuntime(658):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 11:53:50.539: E/AndroidRuntime(658):  at android.os.Looper.loop(Looper.java:137)
09-29 11:53:50.539: E/AndroidRuntime(658):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-29 11:53:50.539: E/AndroidRuntime(658):  at java.lang.reflect.Method.invokeNative(Native Method)
09-29 11:53:50.539: E/AndroidRuntime(658):  at java.lang.reflect.Method.invoke(Method.java:511)
09-29 11:53:50.539: E/AndroidRuntime(658):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-29 11:53:50.539: E/AndroidRuntime(658):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-29 11:53:50.539: E/AndroidRuntime(658):  at dalvik.system.NativeStart.main(Native Method)

我的 mainactivity.java

public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = getLayoutInflater();

            View row = inflater.inflate(R.layout.row, parent, false);

            TextView textview = (TextView) row.findViewById(R.id.tvTop);
            ImageView imageview = (ImageView) row
                    .findViewById(R.id.list_image);

            textview.setText(data_topText[position]);
            imageview.setImageResource(data_image[position]);

            row.setOnClickListener(new OnClickListener() {

                public void onClick(View convertView) {

                    String product = ((TextView) convertView).getText().toString();

                    Intent i = new Intent(getApplicationContext(),
                            SecondScreenActivity.class);
                    i.putExtra("name", product);

                    startActivity(i);


                }
            });

            return (row);
4

2 回答 2

2
String product = ((TextView) convertView).getText().toString();

在这里,convertView不是一个TextView. 这是一个LinearLayout

你可能想要这个:

String product = ((TextView) convertView.findViewbyId(R.id.tvTop)).getText().toString();
于 2012-09-29T04:07:16.703 回答
1

你要这个

public void onClick(View row) {
           TextView textview = (TextView) row.findViewById(R.id.tvTop);

            String product = textView.getText().toString();

            Intent i = new Intent(getApplicationContext(),
                    SecondScreenActivity.class);
            i.putExtra("name", product);

            startActivity(i);


        }

您正在尝试将您的布局转换为 TextView。接收点击的是 LinearLayout。

于 2012-09-29T04:06:25.450 回答