2014-01-17 88 views
1

我有一個自定義適配器列表視圖。其佈局如下Android動態創建的適配器列表視圖中的getText

RelativeLayout 
    LinearLayout 
     TextView 
     Button 

需要在該按鈕上添加單擊事件。在這種情況下,需要從該TextView獲取文本並將其傳遞給下一個活動。

我得到了下面的代碼

holder.btn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View vp) { 

     LinearLayout rl = (LinearLayout)vp.getParent(); 
     TextView tv = (TextView)rl.findViewById(R.id.name); 
     String text = tv.getText().toString(); 

    } 
}); 

但它崩潰的String text = tv.getText().toString();

任何想法?

更新

01-17 19:59:12.877: E/AndroidRuntime(16659): FATAL EXCEPTION: main 
01-17 19:59:12.877: E/AndroidRuntime(16659): Process: com.atc.detectoract, PID: 16659 
01-17 19:59:12.877: E/AndroidRuntime(16659): java.lang.NullPointerException 
01-17 19:59:12.877: E/AndroidRuntime(16659): at com.atc.detectoract.CustomAdapter$2.onClick(CustomAdapter.java:129) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at android.view.View.performClick(View.java:4438) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at android.view.View$PerformClick.run(View.java:18422) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at android.os.Handler.handleCallback(Handler.java:733) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at android.os.Handler.dispatchMessage(Handler.java:95) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at android.os.Looper.loop(Looper.java:136) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at android.app.ActivityThread.main(ActivityThread.java:5017) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at java.lang.reflect.Method.invokeNative(Native Method) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at java.lang.reflect.Method.invoke(Method.java:515) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
01-17 19:59:12.877: E/AndroidRuntime(16659): at dalvik.system.NativeStart.main(Native Method) 
+0

發表您的logcat請 –

+0

添加目錄下載(我假設的「電視」的NPE),也是佈局XML,不只是解釋。 – s1m3n

+0

更新了logcat – ramesh

回答

1
But it is crashing on String text = tv.getText().toString(); 

你有NullPointerExceptiontv爲空

您可以使用setTaggetTag

holder.btn.setTag(holder.tv.getText().toString()); // set tag 

然後

holder.btn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View vp) { 

     String value = (String)vp.getTag(); // get tag 

    } 
}); 
+0

這工作:)謝謝 – ramesh

+0

@ramesh繼續,並接受答案,如果它有幫助 – Raghunandan

+0

是的..當然...等待9分鐘接受:) – ramesh