单击表格行中的textView时出错

时间:2016-01-06 09:29:04

标签: java android android-layout

我有一个包含textviews的表格行 我试图使用意图点击文本视图移动到下一个活动,但当我点击文本视图时,我收到以下错误

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"

      />

3 个答案:

答案 0 :(得分:0)

当您使用OnClickListener时,您不必实施android:onClick="onClick",只需让public void onClick(View view)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"

        />

SELECT  MS.* FROM 
   SLS_DO_MST MS  LEFT OUTER JOIN INV_ISSUE ISS 
   ON MS.MID = ISS.SLS_DO_MST_MID 
   where ISS.SLS_DO_MST_MID is null

答案 1 :(得分:0)

我认为你是错位的参考名称。

只需更改

    textView.setOnClickListener(this);

//Activity

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 textView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.tablerow_overage);

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

      textView.setOnClickListener(this);

   }

   public void onClick(View view)
   {

      Toast.makeText(this,"Code Working Fine Now Remove Comments Of Below",Toast.LENGTH_LONG).show();
      //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:clickable="true"
android:ellipsize="end"

  />

//。xml变为

# -*- coding: utf-8 -*-
import csv
import MySQLdb
from UnicodeSupportForCsv import UnicodeWriter
import sys
reload(sys)  
sys.setdefaultencoding('utf8')
#sys.setdefaultencoding('Cp1252')

conn = MySQLdb.Connection(db='testdb', host='localhost', user='testuser', passwd='testpasswd', use_unicode=0,charset='utf8')

crsr = conn.cursor()
crsr.execute("SELECT * FROM rfid")

with open(r'test.csv', 'wb') as csvfile:
    uw = UnicodeWriter(
        csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)

    for row in crsr.fetchall():
        uw.writerow([unicode(col) for col in row])

答案 2 :(得分:0)

我认为您没有正确初始化tv_number

您创建了一个未使用的本地变量TextView textView,可能会让您误以为您初始化tv_number

请替换

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

使用

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