gpt4 book ai didi

java - 单击表格行中的textView时出错

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:25 25 4
gpt4 key购买 nike

我有一个包含 TextView 的表行我尝试使用 Intent 单击 TextView 以移至下一个 Activity ,但是当我单击 TextView 时出现以下错误

java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'tv_msisdn'
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4479)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4443)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)

代码聆听

package com.tela.mobile.home;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.tela.mobile.R;

public class DeviceAlertDetail extends Activity implements OnClickListener
{
private TextView tv_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.tablerow_overage);

TextView textView = (TextView)findViewById(R.id.tv_number);

tv_number.setOnClickListener(this);

}
public void onClick(View view)
{
Intent intent = new Intent(this, SearchResultsActivity.class);
this.startActivity(intent);
}

}

我在布局文件中为textView设置onClick,如下

 <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:padding="1dp"
android:text="number"
android:id="@+id/tv_number"
android:textSize="12sp"
android:maxLines="1"
android:onClick="onClick"
android:clickable="true"
android:ellipsize="end"

/>

最佳答案

当您使用android:onClick="onClick"时,不必实现OnClickListener,只需让public void onClick(View view)作为它,并从 Activity 中删除implements OnClickListener

你的代码会像

package com.tela.mobile.home;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.tela.mobile.R;

public class DeviceAlertDetail extends Activity
{
private TextView tv_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.tablerow_overage);

}
public void onClick(View view)
{
Intent intent = new Intent(this, SearchResultsActivity.class);
this.startActivity(intent);
}

}

 <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:padding="1dp"
android:text="number"
android:id="@+id/tv_number"
android:textSize="12sp"
android:maxLines="1"
android:onClick="onClick"
android:clickable="true"
android:ellipsize="end"

/>

关于java - 单击表格行中的textView时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com