尝试提取纯文本数据以在服务中使用
Trying to pull plain text data to use in a service
我正在为 android 移动数据终端创建一个应用程序,它可以获取扫描的数据并将指定字符转换为另一个字符。目前的工作方式,我手动编码要查找的字符并将其转换为:
public String specialWorkFor(String originalData, String codeType ){
String[] targetStr = {"1", "2", "3"};
String[] replaceStr = {"a","b","c"};
String newData = "";
newData = originalData;
newData = ReplaceText(originalData,targetStr,replaceStr);
return newData;
}
private static String ReplaceText (String originalData, String[] targetStr, String[] replaceStr ){
String newData = "";
String newDataTmp = "";
newData = originalData;
for (int i = 0; i < targetStr.length; i++){
newDataTmp = newData.replace(targetStr[i], replaceStr[i]);
newData = newDataTmp;
}
return newData;
}
这很好用,但理想情况下,我希望有一个界面,我可以在其中键入纯文本字段并使用那里的值来确定要转换的字符。
创建布局后,我尝试这样做:
//Inputs
Context context1 = getApplicationContext();
Activity act1=(Activity)context1;
EditText codein = (EditText) act1.findViewById(R.id.input);
String in = codein.getText().toString();
//Outputs
Context context2 = getApplicationContext();
Activity act2=(Activity)context2;
EditText codeout = (EditText) act2.findViewById(R.id.output);
String out = codeout.getText().toString();
public String specialWorkFor(String originalData, String codeType ){
//String[] targetStr = {in};
//String[] replaceStr = {out};
String newData = "";
newData = originalData;
newData = ReplaceText(originalData,targetStr,replaceStr);
return newData;
}
我使用的地方:
Context context1 = getApplicationContext();
Activity act1=(Activity)context1;
EditText codein = (EditText) act1.findViewById(R.id.input);
String in = codein.getText().toString();
从我输入值的 activity 中提取值。问题是当我扫描时出现错误:
尝试在空对象引用上调用虚方法'android.content.Context android.content.Context.getApplicationContext()'
我不确定从这里到哪里去。有什么想法吗?
您不能将应用程序上下文转换为 activity。
像这样创建 class 存储:
public class Storage {
public static final Storage instance = new Storage();
public String codeIn = "";
public String codeOut = "";
}
向两个 edittext 添加一个 textWatcher,并在 ontextchanged 中添加:
Storage.instance.codeIn = whatever is in coresponding edittext
在服务中,您可以通过相同的方式获取这些值:
String in = Storage.instance.codeIn
另请注意,必须在服务使用它们之前设置这些值)))
我正在为 android 移动数据终端创建一个应用程序,它可以获取扫描的数据并将指定字符转换为另一个字符。目前的工作方式,我手动编码要查找的字符并将其转换为:
public String specialWorkFor(String originalData, String codeType ){
String[] targetStr = {"1", "2", "3"};
String[] replaceStr = {"a","b","c"};
String newData = "";
newData = originalData;
newData = ReplaceText(originalData,targetStr,replaceStr);
return newData;
}
private static String ReplaceText (String originalData, String[] targetStr, String[] replaceStr ){
String newData = "";
String newDataTmp = "";
newData = originalData;
for (int i = 0; i < targetStr.length; i++){
newDataTmp = newData.replace(targetStr[i], replaceStr[i]);
newData = newDataTmp;
}
return newData;
}
这很好用,但理想情况下,我希望有一个界面,我可以在其中键入纯文本字段并使用那里的值来确定要转换的字符。
创建布局后,我尝试这样做:
//Inputs
Context context1 = getApplicationContext();
Activity act1=(Activity)context1;
EditText codein = (EditText) act1.findViewById(R.id.input);
String in = codein.getText().toString();
//Outputs
Context context2 = getApplicationContext();
Activity act2=(Activity)context2;
EditText codeout = (EditText) act2.findViewById(R.id.output);
String out = codeout.getText().toString();
public String specialWorkFor(String originalData, String codeType ){
//String[] targetStr = {in};
//String[] replaceStr = {out};
String newData = "";
newData = originalData;
newData = ReplaceText(originalData,targetStr,replaceStr);
return newData;
}
我使用的地方:
Context context1 = getApplicationContext();
Activity act1=(Activity)context1;
EditText codein = (EditText) act1.findViewById(R.id.input);
String in = codein.getText().toString();
从我输入值的 activity 中提取值。问题是当我扫描时出现错误:
尝试在空对象引用上调用虚方法'android.content.Context android.content.Context.getApplicationContext()'
我不确定从这里到哪里去。有什么想法吗?
您不能将应用程序上下文转换为 activity。 像这样创建 class 存储:
public class Storage {
public static final Storage instance = new Storage();
public String codeIn = "";
public String codeOut = "";
}
向两个 edittext 添加一个 textWatcher,并在 ontextchanged 中添加:
Storage.instance.codeIn = whatever is in coresponding edittext
在服务中,您可以通过相同的方式获取这些值:
String in = Storage.instance.codeIn
另请注意,必须在服务使用它们之前设置这些值)))