当我运行我的应用程序时,我从标题中获得了异常.它的作用是它有一个带有Hangman游戏单词的.txt文件,我认为在访问该文件时会抛出异常.我的文件cuvinte.txt位于/ assets /中.这是我的代码(我跳过layout/xml部分,工作正常):
public void onCreate() {
// all the onCreate() stuff, then this:
try {
AssetManager am = this.getAssets();
InputStream is = am.open("cuvinte.txt");
InputStreamReader inputStreamReader = new InputStreamReader(is);
BufferedReader b = new BufferedReader(inputStreamReader);
String rand;
while((rand=b.readLine())!=null){
cuvinte.add(rand);
}
} catch (IOException e) {
Toast.makeText(this, "No words file", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
newGame(newG);
}
public void newGame(View view){
Random rand = new Random();
String stringCuvant = cuvinte.get(rand.nextInt(cuvinte.size()));
cuvant.setText("");
System.out.println(stringCuvant);
for(int i = 0; i< stringCuvant.length(); i++){
cuvant.append("_ ");
}
incercari.setText(valIncercari);
} …Run Code Online (Sandbox Code Playgroud) 我有一个返回char数组的函数,我希望它变成一个String,这样我就可以更好地处理它(与其他存储数据相比).我使用这个简单的应该工作,但它不是出于某种原因(bufferPos是数组的长度,buffer是数组,item是一个空字符串):
for(int k=0; k<bufferPos; k++){
item += buffer[k];
}
Run Code Online (Sandbox Code Playgroud)
在buffer有正确的价值观也是如此bufferPos,但是当我尝试转换,例如544900010837154,它仅持有54如果我添加Serial.prints到像这样:
for(int k=0; k<bufferPos; k++){
Serial.print(buffer[k]);
Serial.print("\t");
Serial.println(item);
item += buffer[k];
}
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
5
4 5
4 54
9 54
0 54
0 54
0 54
1 54
0 54
8 54
3 54
7 54
1 54
Run Code Online (Sandbox Code Playgroud)
我错过了什么?感觉就像这么简单的任务,我没有看到解决方案......
当我在AVD Manager中创建一个新的虚拟设备时,在目标下拉菜单中我只有两个选项可用:API级别8和17(2.2和4.2.2),尽管我已经安装了所有API,其中包含所有子组件.更重要的是,如果我选择4.2.2,则禁用"确定"按钮.
如何将目标API添加到AVD Manager?它们不是直接链接到我安装的SDK吗?
我在主要活动中有一个自定义BaseAdapter和一个添加按钮.该按钮打开一个带有文本框的对话框,您可以通过该方式向列表中添加新元素.问题是列表没有刷新.在onActivityResult()函数中,我打印列表中的元素数量,每次在对话框中点击"确定"时,数字都会增加,所以我知道这只是刷新不起作用.我的BaseAdapter和我的活动:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
this.notifyDataSetChanged();
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
return element;
}
}
public class WeatherAppActivity extends ListActivity {
Button buton;
ListaOrase lista;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lista=new …Run Code Online (Sandbox Code Playgroud) 我对Android很新,只是熟悉常见的东西,但我无法掌握onClickListner(); 我基本上有两个复选框和一个按钮,然后在按钮上单击一个吐司应显示并说明哪些复选框已选中,哪些不是.
public class ExActivity extends Activity implements View.OnClickListener {
CheckBox cb;
CheckBox cb2;
Button buton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cb=(CheckBox) findViewById(R.id.cb);
cb2=(CheckBox) findViewById(R.id.checkbox);
buton = (Button)findViewById(R.id.buton);
buton.setOnClickListener(this);
}
public void onClick(View arg0) {
Toast toast;
if(cb.isChecked()&&cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Amandoua sunt bifate", Toast.LENGTH_SHORT);
else if(cb.isChecked()&&!cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Doar prima e bifata", Toast.LENGTH_SHORT);
else if(!cb.isChecked()&&cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Doar a doua e bifata", Toast.LENGTH_SHORT);
else if(!cb.isChecked()&&!cb2.isChecked()) toast = Toast.makeText(getApplicationContext(), "Nici una nu e bifata", Toast.LENGTH_SHORT);
} …Run Code Online (Sandbox Code Playgroud) 我正在努力制作的应用程序有一个简单的列表和一个按钮存在于底部,无论如何.我的问题是我的自定义BaseAdapter不显示元素.我知道,因为我的元素只是一个字符串,我可以使用ArrayAdapter,但是赋值需要它.代码:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater …Run Code Online (Sandbox Code Playgroud) 我想创建一个简单的应用程序,显示联系人列表(姓名,姓氏).我的代码:
package lista.android;
import java.util.*;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.*;
class Contact{
String nume;
String prenume;
Contact(String nume, String prenume){
this.nume=nume;
this.prenume=prenume;
}
public String toString(){
return prenume +" "+ nume;
}
}
public class Lista1Activity extends ListActivity {
/** Called when the activity is first created. */
ArrayList <Contact> lista;
ArrayAdapter <Contact> adaptor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(adaptor);
lista=new ArrayList<Contact>();
adaptor=new ArrayAdapter<Contact>(this, R.id.element, lista);
adaugaContact("Florian", "Iancu");
adaugaContact("Ioana", "Constantina");
}
public void adaugaContact(String nume, String …Run Code Online (Sandbox Code Playgroud)