Good morning, I have a layout with some checkbox, these are disabled so that the user can not mark them because for security reasons this check must occur via code, I have the following code: I need to set the checkbox as checked via java code and not via XML, because I will build a logic with this information.
public class ZXingJarDemoActivity extends Activity {
private static final String editText1 = null;
protected static final TextView Dispositivo = null;
private static final EditText bscdispo = null;
private CheckBox d2201;
private Handler handler = new Handler();
private TextView txtScanResult;
private TextView textbox1;
private View validado;
private TextView output;
private EditText bscdispo1;
private String buscar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
d2201=(CheckBox)findViewById(R.id.d2201);
bscdispo1= (EditText) findViewById(R.id.bscdispo1);
txtScanResult = (TextView) findViewById(R.id.scan_result);
textbox1 = (TextView) findViewById(R.id.scan_result2);
d2201.setChecked(true);
When I run the application it opens and closes giving crash, if I remove the code "d2201.setChecked (true); it works normally.
LogCat - Updated
Activy Complete
package msinfomg.leitorcodigobarras;
import msinfomg.codebar.leitor.R;
public class ZXingJarDemoActivity extends Activity {
private static final String editText1 = null;
protected static final TextView Dispositivo = null;
private static final EditText bscdispo = null;
private CheckBox d2201;
private Handler handler = new Handler();
private TextView txtScanResult;
private TextView textbox1;
private View validado;
private TextView output;
private EditText bscdispo1;
private String buscar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
d2201=(CheckBox)findViewById(R.id.d2201);
bscdispo1= (EditText) findViewById(R.id.bscdispo1);
txtScanResult = (TextView) findViewById(R.id.scan_result);
textbox1 = (TextView) findViewById(R.id.scan_result2);
d2201.setChecked(true);
View botao = findViewById(R.id.Botao);
botao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator.initiateScan(ZXingJarDemoActivity.this, R.layout.capture,
R.id.viewfinder_view, R.id.preview_view, true);
}
});
}
// BOTÃO BUSCAR - BUSCA O DISPOSITIVO QUE ESTA DIGITADO NO CAMPO DE BUSCA
public void scan_dispositivo(View v) {
buscar = bscdispo1.getText().toString();
if ( buscar.contentEquals("7119")) {
setContentView(R.layout.d7119);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode,
resultCode, data);
if (scanResult == null) {
return;
}
final String result = scanResult.getContents();
if (result != null) {
handler.post(new Runnable() {
@Override
public void run() {
if ( ((String) txtScanResult.getText()).trim().equals("") ){
txtScanResult.setText(result);
} else {
textbox1.setText(result);
}
if ( !((String) txtScanResult.getText()).trim().equals("") && ((String) textbox1.getText()).trim().equals("") ){
IntentIntegrator.initiateScan(ZXingJarDemoActivity.this, R.layout.capture2,
R.id.viewfinder_view, R.id.preview_view, true);
}
if ( !((String) txtScanResult.getText()).trim().equals("") && !((String) textbox1.getText()).trim().equals("") ){
if ( (txtScanResult.getText().toString().trim()+"A").equals(textbox1.getText().toString()) ){
setContentView(R.layout.verdade);
// String lstrNomeArq;
byte[] dados;
// lstrNomeArq = (txtScanResult.getText() + ".txt");
File arquivo = new File(Environment.getExternalStorageDirectory(), "Banco_consig");
dados = txtScanResult.getText().toString().getBytes();
try {
if (!arquivo.exists()) {
//cria um arquivo (vazio)
arquivo.createNewFile();
}
//caso seja um diretório, é possível listar seus arquivos e diretórios
File[] arquivos = arquivo.listFiles();
//escreve no arquivo
FileWriter fw = new FileWriter(arquivo, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write((String) txtScanResult.getText());
bw.newLine();
bw.close();
fw.close();
//faz a leitura do arquivo
FileReader fr = new FileReader(arquivo);
BufferedReader br = new BufferedReader(fr);
//equanto houver mais linhas
while (br.ready()) {
//lê a proxima linha
String linha = br.readLine();
//faz algo com a linha
System.out.println(linha);
}
br.close();
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
if ( ((String)txtScanResult.getText()).trim().equals("2234") || ((String)txtScanResult.getText()).trim().equals("2235")
|| ((String)txtScanResult.getText()).trim().equals("22401") || ((String)txtScanResult.getText()).trim().equals("22402")
|| ((String)txtScanResult.getText()).trim().equals("22403") || ((String)txtScanResult.getText()).trim().equals("22404")
|| ((String)txtScanResult.getText()).trim().equals("2464") || ((String)txtScanResult.getText()).trim().equals("2497")
|| ((String)txtScanResult.getText()).trim().equals("2201") || ((String)txtScanResult.getText()).trim().equals("2202")
|| ((String)txtScanResult.getText()).trim().equals("2203") || ((String)txtScanResult.getText()).trim().equals("2423")
|| ((String)txtScanResult.getText()).trim().equals("24451") || ((String)txtScanResult.getText()).trim().equals("2207")
|| ((String)txtScanResult.getText()).trim().equals("2212") || ((String)txtScanResult.getText()).trim().equals("2216")
|| ((String)txtScanResult.getText()).trim().equals("1299")) {
String dispo7119 = "7119";
EditText Dispositivo = (EditText) findViewById(R.id.dispositivo);
Dispositivo.setText(dispo7119);
} else {
String desc = "NULL";
EditText Dispositivo = (EditText) findViewById(R.id.dispositivo);
Dispositivo.setText(desc);
}
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
txtScanResult.setText(null);
textbox1.setText(null);
Intent intent = getIntent();
finish();
startActivity(intent);
}
},
1900
);
}
else {
setContentView(R.layout.falso);
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 3545000);
}
}
}
private Object trim() {
// TODO Auto-generated method stub
return null;
}
});
}
break;
default:
}
}
public void voltar(View v) {
txtScanResult.setText(null);
textbox1.setText(null);
Intent intent = getIntent();
finish();
startActivity(intent);
}
public void sobre(View v) {
setContentView(R.layout.sobre);
}
}
Main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fundo_main"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/topo" />
<Button
android:id="@+id/Botao"
android:layout_width="539dp"
android:layout_height="wrap_content"
android:text="@string/scan" />
<TextView
android:id="@+id/scan_result2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
<TextView
android:id="@+id/scan_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:visibility="invisible" />
<EditText
android:id="@+id/bscdispo1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/scandispo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:onClick="scan_dispositivo"
android:text="Acessar Dispositivo" />
<Button
android:id="@+id/sobre"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:onClick="sobre"
android:text="Sobre" />
</LinearLayout>
D7119 file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/EditText01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:background="#789854"
android:ems="10"
android:enabled="false"
android:text=" DISPOSITIVO 7119"
android:textColor="#ffffff"
android:textSize="25sp" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="#789854"
android:ems="10"
android:enabled="false"
android:gravity="end"
android:text="CHAVES:"
android:textColor="#ffffff"
android:textSize="19dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/d2201"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2201" />
<CheckBox
android:id="@+id/d2202"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2202" />
<CheckBox
android:id="@+id/d2203"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2203" />
<CheckBox
android:id="@+id/d2207"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2207" />
<CheckBox
android:id="@+id/d2212"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2212" />
<CheckBox
android:id="@+id/d2216"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2216" />
<CheckBox
android:id="@+id/d2234"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2234" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/d2235"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2235" />
<CheckBox
android:id="@+id/d2445"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2445" />
<CheckBox
android:id="@+id/d2423"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2423" />
<CheckBox
android:id="@+id/d2464"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2464" />
<CheckBox
android:id="@+id/d2497"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="2497" />
<CheckBox
android:id="@+id/d22401"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="22401" />
<CheckBox
android:id="@+id/d22402"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="22402" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15" >
<CheckBox
android:id="@+id/d22403"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="22403" />
<CheckBox
android:id="@+id/d22404"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="22404" />
<CheckBox
android:id="@+id/d24451"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="24451" />
<CheckBox
android:id="@+id/d1299"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="1299" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/footertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:contentDescription="31 8948-9095 Matheus Arruda | Todos os direitos reservados."
android:ems="10"
android:enabled="false"
android:gravity="center"
android:text="MS INFO-DEV 2015"
android:textColor="#FFFFFF"
android:textSize="13dp" />
<Button
android:id="@+id/voltar_index"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:onClick="voltar"
android:text="Voltar" />
</TableRow>
</TableLayout>
</LinearLayout>