我有一个简单的child_process.exec语句,其输出(stdout)由于某种原因在开头总是有一个“♀”字符
var exec = require('child_process').exec;
exec('echo hi', function (err, stdout) {
console.log(stdout);
});
[
我的节点是v0.12,我也安装了iojs,这是v2.3。我已经单独测试了两个相同的结果。我也在不同的控制台测试它 - cmd.exe,powershell和Git的sh.exe,结果相同。
这个角色应该存在吗?如果没有,可能产生什么呢?
答案 0 :(得分:0)
根据stdout
documentation,传递给var exec = require('child_process').exec;
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
exec('echo hi', function (err, stdout) {
var message = decoder.write(stdout);
console.log(message.trim());
});
的对象是缓冲对象。在将其作为字符串打印出来之前,您需要对其进行解码。
我修改了你的代码来演示如何做到这一点。该符号不再出现在我的控制台中。
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.nick.mowen.receiptmanager.LOCATION";
public String Places[];
public RecyclerView RV;
private RVAdapter adapter;
Cursor mCursor;
ManagerDatabaseAdapter managerDatabaseAdapter;
List<MainInfo> mainInfo = new ArrayList<MainInfo>();
RecyclerView.LayoutManager layoutManager;
private Context activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
setContentView(R.layout.activity_main);
adapter = new RVAdapter(getActivity(), managerDatabaseAdapter.getTheCursor());
RV = (RecyclerView) findViewById(R.id.mainV);
layoutManager = new LinearLayoutManager(getActivity());
RV.setLayoutManager(layoutManager);
RV.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// getMenuInflater().inflate(R.menu.menu_main, li)
return true;
}
public static void getData () {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this,SettingsActivity.class);
intent.putExtra(EXTRA_MESSAGE,true);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
return true;
}
return super.onOptionsItemSelected(item);
}
/*@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView userText= (TextView) view;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}*/
public void addInstance(View view) {
Intent intent = new Intent(this,LocationAdder.class);
intent.putExtra(EXTRA_MESSAGE,true);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
}
public Context getActivity() {
return activity;
}
}
有关字符串解码器的更多详细信息,您可以随时查看文档here