2012-11-15 17 views
0

我無法文本字符串從非活性類追加到一個EditText視圖追加一個EditText。從非活性

我試圖傳遞一個視圖作爲參數的類的構造函數。

基本問題是我無法在非活動類中使用findViewById。

我知道這是一個愚蠢的問題,但我嘗試了很多,但根本無法得到。

我的代碼示例是:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.printtesting; 

import android.app.Activity; 
import android.widget.EditText; 
import java.math.BigInteger; 
import java.util.Random; 
import java.util.Vector; 
//import javax.swing.JOptionPane; 

/** 
* 
* @author HP 
*/ 
public class keyGenerator { 

    /** 
    * @param args the command line arguments 
    */ 

     static Vector check = new Vector(); 


     static protected BigInteger p= new BigInteger("161"); 
     static protected BigInteger q= new BigInteger("47"); 
     static protected Random s = new Random(); 
     static protected BigInteger n = new BigInteger("1"); 
     static protected BigInteger trails; 
     static protected BigInteger lambda ; 
     static protected BigInteger nsq = new BigInteger("1"); 
     static protected BigInteger g = new BigInteger("1"); 
     static protected BigInteger temp = new BigInteger("1"); 

    static protected int maxbit; 
    private static BigInteger two = new BigInteger("2"); 

    public keyGenerator() { 
     // p = new BigInteger(7,1,s); 
       System.out.println(p.toString()); 
     /* while(!isPrime(p) && ((p.compareTo(two)==1) || (p.compareTo(two))==0)) 
     { 
       p = new BigInteger(7,1,s); 

     System.out.println(p.toString()); 
     }*/ 
     System.out.println("P is " + p); 


    // q = new BigInteger(7,1,s); 
     /* while(!isPrime(q) && ((q.compareTo(two)==1) || (q.compareTo(two))==0)) 
     { 
       q = new BigInteger(7,1,s); 
     }*/ 
     System.out.println("Q is " + q); 




     // TODO code application logic here 

     // BigInteger oth = new BigInteger("132312"); 
     generateKey(); 

    } 


    protected void generateKey() 
    { 
    EditText et = (EditText) Activity.findViewByID(R.string.te); 
    // N=pq 
    n=n.multiply(p); 
    n=n.multiply(q); 
    .... 
} 
+0

爲什麼不將EditText **內容**傳遞給keyGenerator類?將View發送到非活動類時必須小心,因爲如果非活動類超出活動的使用期限,可能會導致泄漏 – nicopico

回答

0

你可以通過EDITTEXT活動課的背景下,以非的KeyGenerator類爲:

Context conn; 
public keyGenerator(Context con) { 
conn=con; 
//Your Code... 
} 

現在,您可以附加上的KeyGenerator文字EDITTEXT爲:

EditText et = (EditText)conn.findViewByID(R.string.te); 
et.append("your text here"); 

你可以通過電流keyGenerator構造函數的上下文爲:

keyGenerator obj=new keyGenerator(Current_Activity.this); 
+0

非常感謝!!請過answet他的問題! http://stackoverflow.com/questions/13339542/can not-bluetooth-adapter-in-android http://stackoverflow.com/questions/13226072/unable-to-implement-wifidirectdemo-sample-in-android – 10101010

2

你不能這樣做,因爲findViewByID是一個實例方法,而不是一個靜態方法。你需要做兩件事情之一:

1)傳遞活動本類作爲參數。這將允許你調用它的findViewByID

2)傳遞EditText作爲參數(在activity類中調用findViewByID之後)。這將允許你只需要調用setText

+0

您的意思是? protected void generateKey(Activity a) { EditText et =(EditText)a.findViewByID(R.string.te); // N = pq n = n.multiply(p); n = n.multiply(q); 仍然給出了一個錯誤:( – 10101010

+0

看起來像的方式第二次工作!就讓我做最後的驗證! – 10101010

0
EditText et = (EditText) Activity.findViewByID(R.string.te); 

你需要從activity中傳遞活動var。

EditText et = (EditText) (activity var).findViewByID(R.string.te);