Reputation: 51
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
I want user perform some activities on Textview Click, but that part is not invoked properly in my program.Please see the XML File, Java Program and also the particular function of clickable event.
XML File:
<?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>
I want user to click the Text view also, so that i can do some function. But the below module in the program is not called.Java Program which contains the above code, the CellTextClick should be called in the below program, but its not invoked.
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());
}
}
}
Upvotes: 0
Views: 631
Reputation: 1684
Give id to your 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"/>
Activity:
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;
}
}
});
Hope it will work !!
Updated Answer:--
Write this,
TextView tv1 = (TextView) findViewById(R.id.row_cell_text_dummy_multilevel);
instead of this
TextView textview = (TextView) findViewById(R.id.row_cell_text_multilevel);
As you had give your button id
to your textView
, thats why you are getting null pointer exception...
Upvotes: 0
Reputation: 1495
Try to give an id to the textview and try again or Try this:
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();
});
Upvotes: 0
Reputation: 4569
Add id to the 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"/>
And add onclick listener
TextView textview = (TextView) findViewById(R.id.textview01);
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Text clicked");
}
});
Upvotes: 0
Reputation: 800
try it out
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");
}
});
Upvotes: 1
Reputation:
Try this
TextView textview = (TextView) findViewById(R.id.yourid);
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 1
Reputation: 4691
try following
<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"/>
Activity
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;
}
}
});
Upvotes: 0