TEXTVIEW Click在android中不起作用

时间:2014-02-25 05:05:28

标签: android onclick textview

package com.jd.multileveltreelistview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MultilevelTreeListView extends Activity 
{
    ListAdapter adapter;
    ListView mainList;
    ArrayList<Entity>arrTrades;
    public static int totcnt = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.treelist);
        mainList=(ListView)findViewById(R.id.currentpending_list);
        mainList.setDividerHeight(4);
        arrTrades=new ArrayList<Entity>();

        try {
        TextView textview = (TextView) findViewById(R.id.row_cell_text_multilevel);
        textview.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(), "Answer As of Now : " , Toast.LENGTH_SHORT).show(); 
                        }
                 });
        } catch(Exception e) { }
    }

    @Override
    protected void onResume() 
    {
        if(arrTrades.size()==0){
            populateList();
        }
        super.onResume();
    }

    public Entity getEntity(int level,int haschild){
        Entity E=new Entity();
        E.Name="Level "+level;
        E.Name= " S.Viswanathan " + totcnt + "\n";
        E.Name = E.Name + "Role: Group Leader " + "\n";
        E.Name = E.Name + "Mail: s.vishy@tcscom " + "\n";
        E.Name = E.Name + "Phone:  9600421518";
        totcnt = totcnt + 1;
        E.isOpened=false;
        E.level=level;
        E.HasChild=haschild;
        return E;
    }
    public void populateList()
    {
        try
        {
            for(int i=0;i<1;i++){
                    arrTrades.add(getEntity(0, 1));
            }
            adapter=new ListAdapter(MultilevelTreeListView.this, R.id.row_cell_text_multilevel, arrTrades);
            mainList.setAdapter(adapter);
        }
        catch (Exception e)
        {
            Log.d(" populateList Exception",""+e.getMessage());
        }
    }

    public void CellTextClick()
    {
        Toast.makeText(getApplicationContext(), "Answer As of Now : " , Toast.LENGTH_SHORT).show();
    }
    public void CellButtonClick(View v){
        try{
            Toast.makeText(getApplicationContext(), "Button Clicked : " , Toast.LENGTH_SHORT).show();
            Button b=(Button)v;
            int index;
            index=(Integer) b.getTag();
            if(b.getText().toString().equals("+")){
                b.setText("-");
                Entity temp[]=new Entity[5];
                int PLevel=arrTrades.get(index).level+1;
                for(int i=0;i<5;i++){
                    temp[i]=getEntity(PLevel, 1);
                }
                arrTrades.get(index).isOpened=true;
                if(temp!=null){
                    int addindex=index+1;
                    for(int i=0;i<temp.length;i++){
                        arrTrades.add(addindex, temp[i]);
                        addindex++;
                    }
                }
                temp=null;
            }
            else{
                b.setText("+");
                arrTrades.get(index).isOpened=false;
                    int removeindex=index+1;
                    for(int i=0;i<5;i++){
                        if(arrTrades.get(removeindex).isOpened){
                            removeChilds(removeindex);
                        }
                        arrTrades.remove(removeindex);
                    }
                }


            adapter.notifyDataSetChanged();
        }
        catch(Exception e){
            adapter.notifyDataSetChanged();
            Log.d("Error=", ""+e.getMessage());
        }
    }
    public void removeChilds(int index){
        try {
                int removeindex=index+1;
                for(int i=0;i<5;i++){
                    if(arrTrades.get(removeindex).isOpened){
                        removeChilds(removeindex);
                    }
                    arrTrades.remove(removeindex);
                }
        } catch (Exception e) {
            // TODO: handle exception
            Log.d("Errro=", ""+e.getMessage());
        }
    }
}

ERROR:

02-25 11:34:50.965: E/AndroidRuntime(6907): Uncaught handler: thread main exiting due to uncaught exception
02-25 11:34:50.980: E/AndroidRuntime(6907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jd.multileveltreelistview/com.jd.multileveltreelistview.MultilevelTreeListView}: java.lang.NullPointerException
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.os.Looper.loop(Looper.java:123)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at java.lang.reflect.Method.invokeNative(Native Method)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at java.lang.reflect.Method.invoke(Method.java:521)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at dalvik.system.NativeStart.main(Native Method)
02-25 11:34:50.980: E/AndroidRuntime(6907): Caused by: java.lang.NullPointerException
02-25 11:34:50.980: E/AndroidRuntime(6907):     at com.jd.multileveltreelistview.MultilevelTreeListView.onCreate(MultilevelTreeListView.java:29)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-25 11:34:50.980: E/AndroidRuntime(6907):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-25 11:34:50.980: E/AndroidRuntime(6907):     ... 11 more

我希望用户在Textview Click上执行一些活动,但是在我的程序中没有正确调用该部分。请参阅XML文件,Java程序以及可点击事件的特定功能。

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="40dp"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:gravity="center"

        android:id="@+id/row">
           <HorizontalScrollView 
            android:id="@+id/scrollview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:fillViewport="true"
            android:scrollbars="none">
            <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="0dp"
        android:textSize="10dp"
        android:textColor="#ffffff"
        android:id="@+id/row_cell_text_dummy_multilevel"
        android:singleLine="false"  />
    <Button 
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/row_cell_btn_multilevel"
        android:onClick="CellButtonClick"

        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>
    <TextView 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_marginLeft="7dp"
        android:textSize="10dp"
        android:clickable="true"
        android:onClick="CellTextClick"
        android:textColor="#000000"
        android:layout_weight="4"
        android:singleLine="false"
        android:gravity="center|left"/>
      </LinearLayout> 
      </HorizontalScrollView>
</LinearLayout>

我希望用户也单击文本视图,以便我可以执行某些功能。但是程序中的下面的模块没有被调用.Java程序包含上面的代码,CellTextClick应该在下面的程序中调用,但是它没有被调用。

package com.jd.multileveltreelistview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MultilevelTreeListView extends Activity 
{
    ListAdapter adapter;
    ListView mainList;
    ArrayList<Entity>arrTrades;
    public static int totcnt = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.treelist);
        mainList=(ListView)findViewById(R.id.currentpending_list);
        mainList.setDividerHeight(4);
        arrTrades=new ArrayList<Entity>();
    }

    @Override
    protected void onResume() 
    {
        if(arrTrades.size()==0){
            populateList();
        }
        super.onResume();
    }

    public Entity getEntity(int level,int haschild){
        Entity E=new Entity();
        E.Name="Level "+level;
        E.Name= " S.Viswanathan " + totcnt + "\n";
        E.Name = E.Name + "Role: Group Leader " + "\n";
        E.Name = E.Name + "Mail: s.vishy@tcscom " + "\n";
        E.Name = E.Name + "Phone:  9600421518";
        totcnt = totcnt + 1;
        E.isOpened=false;
        E.level=level;
        E.HasChild=haschild;
        return E;
    }
    public void populateList()
    {
        try
        {
            for(int i=0;i<1;i++){
                    arrTrades.add(getEntity(0, 1));
            }
            adapter=new ListAdapter(MultilevelTreeListView.this, R.id.row_cell_text_multilevel, arrTrades);
            mainList.setAdapter(adapter);
        }
        catch (Exception e)
        {
            Log.d(" populateList Exception",""+e.getMessage());
        }
    }

    public void CellTextClick(View v)
    {
        Toast.makeText(getApplicationContext(), "Answer As of Now : " , Toast.LENGTH_SHORT).show();
    }

    public void CellButtonClick(View v){
        try{
            Toast.makeText(getApplicationContext(), "Button Clicked : " , Toast.LENGTH_SHORT).show();
            Button b=(Button)v;
            int index;
            index=(Integer) b.getTag();
            if(b.getText().toString().equals("+")){
                b.setText("-");
                Entity temp[]=new Entity[5];
                int PLevel=arrTrades.get(index).level+1;
                for(int i=0;i<5;i++){
                    temp[i]=getEntity(PLevel, 1);
                }
                arrTrades.get(index).isOpened=true;
                if(temp!=null){
                    int addindex=index+1;
                    for(int i=0;i<temp.length;i++){
                        arrTrades.add(addindex, temp[i]);
                        addindex++;
                    }
                }
                temp=null;
            }
            else{
                b.setText("+");
                arrTrades.get(index).isOpened=false;
                    int removeindex=index+1;
                    for(int i=0;i<5;i++){
                        if(arrTrades.get(removeindex).isOpened){
                            removeChilds(removeindex);
                        }
                        arrTrades.remove(removeindex);
                    }
                }


            adapter.notifyDataSetChanged();
        }
        catch(Exception e){
            adapter.notifyDataSetChanged();
            Log.d("Error=", ""+e.getMessage());
        }
    }
    public void removeChilds(int index){
        try {
                int removeindex=index+1;
                for(int i=0;i<5;i++){
                    if(arrTrades.get(removeindex).isOpened){
                        removeChilds(removeindex);
                    }
                    arrTrades.remove(removeindex);
                }
        } catch (Exception e) {
            // TODO: handle exception
            Log.d("Errro=", ""+e.getMessage());
        }
    }
}

6 个答案:

答案 0 :(得分:1)

试试这个

TextView textview = (TextView) findViewById(R.id.yourid);
    textview.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    }
            });

答案 1 :(得分:1)

尝试一下

    Textview tv;
  tv=(TextView)findViewById(R.id.row_cell_text_dummy_multilevel);
    tv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            toast();

        }

        private void toast() {
            // TODO Auto-generated method stub
            Log.i("click", "sucss");
        }
    });

答案 2 :(得分:0)

尝试以下

<TextView
    android:id="@+id/tv1" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginLeft="7dp"
    android:textSize="10dp"
    android:clickable="true"
    android:textColor="#000000"
    android:layout_weight="4"
    android:singleLine="false"
    android:gravity="center|left"/>

活动

TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
switch(v.getId()){
case R.id.tv1:
    Toast.makeText(getApplicationContext(), "Answer As of Now : " , Toast.LENGTH_SHORT).show();
    break;

default:
    break;
                }
       }
});

答案 3 :(得分:0)

尝试为textview提供ID,然后重试 要么 试试这个:

TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
   Toast.makeText(getApplicationContext(), "Answer As of Now : " , Toast.LENGTH_SHORT).show();
});

答案 4 :(得分:0)

id 添加到您的textview
XML: -

<TextView 
                android:id="@+id/txt"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_marginLeft="7dp"
                android:textSize="10dp"
                android:textColor="#000000"
                android:layout_weight="4"
                android:singleLine="false"
                android:gravity="center|left"/>

的活动:

TextView tv1 = (TextView) findViewById(R.id.txt);
tv1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
switch(v.getId()){
case R.id.txt:
    Toast.makeText(getApplicationContext(), "Answer As of Now : " , Toast.LENGTH_SHORT).show();
    break;

default:
    break;
                }
       }
});

希望它能起作用!!

更新的答案: -

写这个,

TextView tv1 = (TextView) findViewById(R.id.row_cell_text_dummy_multilevel);

而不是

 TextView textview = (TextView) findViewById(R.id.row_cell_text_multilevel);

当你将button id提供给textView时,这就是为什么你会得到空指针异常......

答案 5 :(得分:0)

将id添加到textview

<TextView 
        android:id="@+id/textview01"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_marginLeft="7dp"
        android:textSize="10dp"
        android:clickable="true"
        android:onClick="CellTextClick"
        android:textColor="#000000"
        android:layout_weight="4"
        android:singleLine="false"
        android:gravity="center|left"/>

添加onclick侦听器

TextView textview = (TextView) findViewById(R.id.textview01);
textview.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                    System.out.println("Text clicked");

            }
        });