我是初学者,我需要解决这个问题。
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);
答案 0 :(得分:2)
String product = ((TextView) convertView).getText().toString();
此处convertView
不是TextView
。这是一个LinearLayout
。
你可能想要这个:
String product = ((TextView) convertView.findViewbyId(R.id.tvTop)).getText().toString();
答案 1 :(得分: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。