Using OnClickListener with multiple ImageButtons? - android

I'm trying to pull data from a barcode with plain text via zxing using their ScannerViaIntent source code. It works perfectly fine when I set up the code for a single ImageButton but when i set up the other two buttons I receive this error when returning the result from the Barcode Scanner.
Error:
07-18 14:16:00.080: E/AndroidRuntime(9004): java.lang.RuntimeException: Unable to resume activity {com.fmi.inventory/com.fmi.inventory.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=0, data=null} to activity {com.fmi.inventory/com.fmi.inventory.MainActivity}: java.lang.NullPointerException
07-18 14:16:00.080: E/AndroidRuntime(9004): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=0, data=null} to activity {com.fmi.inventory/com.fmi.inventory.MainActivity}: java.lang.NullPointerException
07-18 14:16:00.080: E/AndroidRuntime(9004): at com.fmi.inventory.MainActivity.onActivityResult(MainActivity.java:70)
Code:
package com.fmi.inventory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton button;
ImageButton button1;
ImageButton button2;
EditText editField;
Activity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
button = (ImageButton)findViewById(R.id.scanCubeID);
button1 = (ImageButton)findViewById(R.id.scanEmployeeID);
button2 = (ImageButton)findViewById(R.id.scanConfigID);
button.setOnClickListener(listener);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onResume() {
super.onResume();
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
switch (v.getId()){
case (R.id.scanCubeID):
editField = (EditText)findViewById(R.id.editCubeID);
integrator.initiateScan();
case (R.id.scanEmployeeID):
editField = (EditText)findViewById(R.id.editEmployeeID);
integrator.initiateScan();
case (R.id.scanConfigID):
editField = (EditText)findViewById(R.id.editConfigID);
integrator.initiateScan();
}
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String barcode = scanResult.getContents();
this.editField.setText(barcode);
}
// else continue with any other code you need in the method
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editCubeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/scanEmployeeID"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/scanCubeID"
android:ems="10"
android:hint="#string/edit_cubeid" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editEmployeeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/scanConfigID"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/editCubeID"
android:layout_below="#+id/editCubeID"
android:ems="10"
android:hint="#string/edit_employeeid" />
<EditText
android:id="#+id/editConfigID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/scanConfigID"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/editEmployeeID"
android:layout_below="#+id/editEmployeeID"
android:ems="10"
android:hint="#string/edit_configid" />
<ImageButton
android:id="#+id/scanCubeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/desc"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_scan"/>
<ImageButton
android:id="#+id/scanEmployeeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/desc"
android:layout_below="#+id/scanCubeID"
android:src="#drawable/ic_action_scan"/>
<ImageButton
android:id="#+id/scanConfigID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/desc"
android:layout_below="#+id/scanEmployeeID"
android:src="#drawable/ic_action_scan"/>
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editConfigID"
android:text="#string/button_continue" />
</RelativeLayout>

add break for cases in switch as :
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
switch (v.getId()){
case (R.id.scanCubeID):
editField = (EditText)findViewById(R.id.editCubeID);
integrator.initiateScan();
break ; // add here
case (R.id.scanEmployeeID):
editField = (EditText)findViewById(R.id.editEmployeeID);
integrator.initiateScan();
break ;// add here
case (R.id.scanConfigID):
editField = (EditText)findViewById(R.id.editConfigID);
integrator.initiateScan();
break ; // add here
}
}
};

Related

How to set dynamic string as user select in android?

I'm beginner in Android. I'm trying to make an app where when user tap on button he should go to another activity let say if user tap on button 1 he see new activity with information-A and if he press button 2 he see information-B in same activity he saw information A.
How can I set that.
If I am understanding your question correctly, here's a solution.
You can use the putExtras method of Intent to pass data in key/value pairs to the next activity.
Add the following to activity 1 layout:
<Button
android:id="#+id/btnA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A"
android:onClick="goToActivityFromButtonA" />
<Button
android:id="#+id/btnB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btnA"
android:text="Button B"
android:onClick="goToActivityFromButtonB" />
Add the following methods to Activity 1:
public void goToActivityFromButtonA(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button A");
startActivity(intent);
}
public void goToActivityFromButtonB(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button B");
startActivity(intent);
}
Add the following to Activity 2 layout:
<TextView
android:id="#+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Add the following to the onCreate method of activity 2:
TextView txtText = (TextView)findViewById(R.id.txtText);
txtText.setText(getIntent().getExtras().getString("buttonData"));
I just created a sample project for you:
https://drive.google.com/file/d/0B0S6sddMC_rMSUNieUxkR2lFRzQ/view?usp=sharing
you have first create ui for first activity like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FirstActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="77dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="110dp"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/button1"
android:text="Button 2" />
</RelativeLayout>
ui of second activity like this
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second actvity" />
</LinearLayout>
activity first code where i set index with intent for both button click(you also put string double ling and message with intent)
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id){
case R.id.button1:
Intent i1=new Intent(FirstActivity.this,secondactivity.class);
i1.putExtra("one", 1);
startActivity(i1);
break;
case R.id.button2:
Intent i2=new Intent(FirstActivity.this,secondactivity.class);
i2.putExtra("one", 2);
startActivity(i2);
break;
}
}
}
and second activity i get that integer value to identify which button is clicked and fire operation on that condition
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class secondactivity extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
tv=(TextView)findViewById(R.id.textView1);
Bundle extras = getIntent().getExtras();
int index = extras.getInt("one");
if(index==1){
tv.setText("nformation-A "+index);
}else if(index==2){
tv.setText("nformation-B "+index);
}
}
}

how to give an audio file as input to the google speech to text api?

currently i made a 'speechtotext' app using the google speech to text api.
It takes whatever i speak near the mic as an input and displays the text form of it by sending it to google and getting the result from it.
Well,instead of speaking,i want to give an audio file as the input(which i have previously recorded using 'sound recorder' app) to my "speech to text" app.
This audio file is present in the sdcard of the phone.
Google speech to text api should choose this audio file as the input and then give its text form as the output.
I have pasted my "100% working" code of the xml and the java files for your references.
my xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/textView1"
android:background="#drawable/superim"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/tap_on_the_button_to_speak_"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<ImageButton
android:id="#+id/btnSpeak"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/speak"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/txtText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="0.38"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
/>
</LinearLayout>
my java file:
package com.prann.speechtotextdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
//import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"YOUR DEVICE DOESNT SUPPORT SPEECH TO TEXT \n install google vocie search ",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
}
Is it possible to do what i want to do ?? if so,how can it be done ?????
A detailed explanation would be very appreciated.
Thank you in advance. :-)

Android Async Task Progress Bar onProgressUpdate

I am a new programmer. I have seen lots of tutorials but can't understand what is wrong. I am trying to create a ProgressBar from an Async Task. However it always crashes my application.
Here is the "main" app:
package pt.flag.ensemble;
import java.util.Random;
import pt.flag.ensemble.task.AsyncTaskBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
public class Intervals extends Activity {
private static Intervals instance;
private Context context;
private String answer;
int right_question;
int wrong_question;
int randomInt2;
public ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intervals);
// initializing the sounds
final MediaPlayer sound1 = MediaPlayer.create(Intervals.this,
R.raw.maj2);
final MediaPlayer sound2 = MediaPlayer.create(Intervals.this,
R.raw.maj3);
final MediaPlayer sound3 = MediaPlayer.create(Intervals.this,
R.raw.maj4);
//ProgressBar
instance=this;
ProgressBar progressBar = (ProgressBar) findViewById(R.id.Progressbar);
progressBar.setProgress(0);
// Play Methods
final ImageButton Play = (ImageButton) findViewById(R.id.Play);
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AsyncTaskBar().execute();
Play.setEnabled(false);
// generate random number
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;
randomInt2 = randomInt;
// picking the right sound to play
switch (randomInt) {
case 1:
sound1.start();
answer = "M2";
break;
case 2:
sound2.start();
answer = "M3";
break;
case 3:
sound3.start();
answer = "P4";
break;
}
}
});
//Audio Repeat Methods
Button Repeat = (Button) findViewById(R.id.Repeat);
Repeat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// picking the right sound to play
switch (randomInt2) {
case 1:
sound1.start();
new AsyncTaskBar().execute();
answer = "M2";
break;
case 2:
sound2.start();
new AsyncTaskBar().execute();
answer = "M3";
break;
case 3:
sound3.start();
new AsyncTaskBar().execute();
answer = "P4";
break;
}
}
});
}
//Answering Methods
public void buttonClicked2(View view) {
Button clickedButton = (Button) view;
if (clickedButton.getText().toString().equals(answer)) {
Toast.makeText(Intervals.this, "You are Right!", Toast.LENGTH_LONG)
.show();
right_question = right_question + 1;
final ImageButton Play = (ImageButton) findViewById(R.id.Play);
Play.setEnabled(true);
// Passing results through
SharedPreferences sharedPref = getSharedPreferences(
"INTERVALRIGHTS", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("SCORE", right_question);
editor.commit();
} else {
Toast.makeText(Intervals.this, "You are Wrong!", Toast.LENGTH_LONG)
.show();
wrong_question = wrong_question + 1;
// Passing results through
SharedPreferences sharedPref02 = getSharedPreferences(
"INTERVALWRONGS", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref02.edit();
editor.putInt("SCORE02", wrong_question);
editor.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.intervals, menu);
return true;
}
#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.
switch (item.getItemId()) {
case R.id.action_settings:
Intent intent = new Intent(Intervals.this, IntervalResults.class);
startActivity(intent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
public static Intervals getApp() { return instance; }
}
Here is the AsyncTask class
package pt.flag.eventapp.task;
import pt.flag.eventapp.Main;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.widget.Toast;
public class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> {
int progress_status;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//Toast.makeText(Main.getApp(),"Invoke on PreExecute()", Toast.LENGTH_SHORT).show();
progress_status = 0 ;
//Main.getApp().txt_percentage.setText("downloading 0%");
}
#Override
protected Void doInBackground(Void... params) {
while (progress_status < 100){
progress_status +=2;
publishProgress(progress_status);
SystemClock.sleep(300);
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//Main.getApp().progressBar.setProgress(values[0]);
//Main.getApp().txt_percentage.setText("downloading" + values[0] + "%");
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//Toast.makeText(Main.getApp(), "Invoke onPostExecute()", Toast.LENGTH_SHORT).show();
//Main.getApp().txt_percentage.setText("download complete");
}
}
Here is the Log:
08-10 20:38:11.081: E/AndroidRuntime(21441): FATAL EXCEPTION: main
08-10 20:38:11.081: E/AndroidRuntime(21441): java.lang.NullPointerException
08-10 20:38:11.081: E/AndroidRuntime(21441): at pt.flag.ensemble.task.AsyncTaskBar.onProgressUpdate(AsyncTaskBar.java:34)
08-10 20:38:11.081: E/AndroidRuntime(21441): at pt.flag.ensemble.task.AsyncTaskBar.onProgressUpdate(AsyncTaskBar.java:1)
08-10 20:38:11.081: E/AndroidRuntime(21441): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:618)
08-10 20:38:11.081: E/AndroidRuntime(21441): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 20:38:11.081: E/AndroidRuntime(21441): at android.os.Looper.loop(Looper.java:156)
08-10 20:38:11.081: E/AndroidRuntime(21441): at android.app.ActivityThread.main(ActivityThread.java:4977)
08-10 20:38:11.081: E/AndroidRuntime(21441): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 20:38:11.081: E/AndroidRuntime(21441): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 20:38:11.081: E/AndroidRuntime(21441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-10 20:38:11.081: E/AndroidRuntime(21441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-10 20:38:11.081: E/AndroidRuntime(21441): at dalvik.system.NativeStart.main(Native Method)
If I am correct, the log seems to indicate an error in line 34 at the AsyncTask class, which is "Intervals.getApp().progressBar.setProgress(values[0]);" I just don't know why...
Also, here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="#+id/Play"
android:contentDescription="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp" />
<Button
android:id="#+id/Repeat"
android:contentDescription="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Repeat"
android:layout_marginTop ="50dp"
android:layout_toRightOf="#+id/Play"
android:layout_marginLeft="25dp" />
<ProgressBar
android:id="#+id/Progressbar"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"
android:layout_marginTop="20dp"
android:layout_below="#+id/Play" />
<Button
android:id="#+id/Octave_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_marginLeft="5dp"
android:layout_below="#+id/Progressbar"
android:onClick="buttonClicked2"
android:layout_marginTop="100dp"
android:text="#string/octave" />
<Button
android:id="#+id/min2_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Progressbar"
android:onClick="buttonClicked2"
android:layout_marginTop="100dp"
android:layout_toRightOf="#+id/Octave_Button"
android:text="#string/minor2" />
<Button
android:id="#+id/Maj2_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Progressbar"
android:onClick="buttonClicked2"
android:layout_marginTop="100dp"
android:layout_toRightOf="#+id/min2_Button"
android:text="#string/major2" />
<Button
android:id="#+id/min3_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Progressbar"
android:onClick="buttonClicked2"
android:layout_marginTop="100dp"
android:layout_toRightOf="#+id/Maj2_Button"
android:text="#string/minor3" />
<Button
android:id="#+id/Maj3_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Progressbar"
android:onClick="buttonClicked2"
android:layout_marginTop="100dp"
android:layout_toRightOf="#+id/min3_Button"
android:text="#string/major3" />
<Button
android:id="#+id/P4_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Progressbar"
android:onClick="buttonClicked2"
android:layout_marginTop="100dp"
android:layout_toRightOf="#+id/Maj3_Button"
android:text="#string/perfect4" />
<Button
android:id="#+id/A4_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Octave_Button"
android:onClick="buttonClicked2"
android:layout_marginTop="15dp"
android:layout_marginLeft="5dp"
android:text="#string/tritone" />
<Button
android:id="#+id/P5_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/min2_Button"
android:onClick="buttonClicked2"
android:layout_marginTop="15dp"
android:layout_toRightOf="#+id/A4_Button"
android:text="#string/perfect5" />
<Button
android:id="#+id/minor6_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Maj2_Button"
android:onClick="buttonClicked2"
android:layout_marginTop="15dp"
android:layout_toRightOf="#+id/P5_Button"
android:text="#string/minor6" />
<Button
android:id="#+id/Major6_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/min3_Button"
android:onClick="buttonClicked2"
android:layout_marginTop="15dp"
android:layout_toRightOf="#+id/minor6_Button"
android:text="#string/major6" />
<Button
android:id="#+id/minor7_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/Maj3_Button"
android:onClick="buttonClicked2"
android:layout_marginTop="15dp"
android:layout_toRightOf="#+id/Major6_Button"
android:text="#string/minor7" />
<Button
android:id="#+id/Major7_Button"
android:layout_width="52dp"
android:layout_height="80dp"
android:layout_below="#+id/P4_Button"
android:onClick="buttonClicked2"
android:layout_marginTop="15dp"
android:layout_toRightOf="#+id/minor7_Button"
android:text="#string/major7" />
I think it's better if you pass the progress bar as an argument to the AsyncTask:
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.Progressbar);
progressBar.setProgress(0);
// Play Methods
final ImageButton Play = (ImageButton) findViewById(R.id.Play);
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AsyncTaskBar task = new AsyncTaskBar();
task.setProgressBar(progressBar);
task.execute();
}
And then on your AsyncTask declare the setProgressBar method:
public class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> {
ProgressBar bar;
public void setProgressBar(ProgressBar bar) {
this.bar = bar;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (this.bar != null) {
bar.setProgress(values[0]);
}
}
You could do the same with the TextView you're trying to set.
In any case, since you mentioned you're new at this you may want to take a look at the Broadcast/Receiver pattern. Here's how it goes:
Start the async task without setting the progress bar or anything.
Define a BroadcastReceiver, instantiate one in your activity and register/unregister it accordingly.
Whenever there's a progress update on your async task just call sendBroadcast with the progress update as an intent extra. You may need to pass a context parameter when instantiating the AsyncTask.
The onHandleIntent method of your app's broadcast receiver (the one you instantiated on step 2) will run on the UI thread, making all those UI updates safe.
Sounds a bit overwhelming? It is at first, but here are the benefits:
It is much cleaner than passing UI objects to an AsyncTask.
You will learn a powerful Android pattern that will come in handy in other endeavours.
It will save you a lot of hassle (and exceptions) if you switch context or your app is low on memory.
Intervals.getApp().progressBar.setProgress(values[0]);
There are two possibilities for the NullPointerException in this line of code:
getApp() returns null
progressBar is null
In order to determine which is the cause, you need to split this into two separate lines:
Intervals intervals = Intervales.getApp();
intervals.progressBar.setProgress(values[0]);
Now set a breakpoint at the first line, step over it, and determine if intervals is null or not.

Android:Inflate exception

So I've been following the Wrox book on how to learn android and have gotten to the part on how to obtain results from an Activity and got the error
11-10 15:50:31.145: E/AndroidRuntime(528): FATAL EXCEPTION: main
11-10 15:50:31.145: E/AndroidRuntime(528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activites/com.example.activites.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class EditTest
So now I now that the EditTest in my xml below is causing the error but I don't know why.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Please enter you name" />
<EditTest
android:id="#+id/txt_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/btn_OK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK" />
</RelativeLayout>
Here is the class that is unable to start.
package com.example.activites;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
public class MainActivity extends Activity {
String tag = "Events";
int request_Code = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode== KeyEvent.KEYCODE_DPAD_CENTER)
{
startActivityForResult(new Intent("com.example.ACTIVITY2"),request_Code);
}
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code){
if(resultCode==RESULT_OK){
Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
Thanks in advance for any help. I'm sure this is a simple solution I'm just very new to the platform.
I'm surprised it compiled:
<EditTest
I think you mean <EditText

Exception in listener attached to multiple buttons in barcode app

Okay, So I am developing an in house barcode scanner for my company to use when we move computers and equipment. I am currently almost through setting up the Zxing Barcode Scanner Via Intent after some trial and error.
Here is what I'm trying to do.
Next to three EditText fields I have Three ImageButtons that when clicked, implement the BarcodeScanner, once scanned returns the value and inputs the value into the EditText field. I was able to do it successfully using one "listener" corresponding to one ImageButton. But after trying to use multiple Buttons with one listener, it calls the barcode scanner but crashes when trying to return the barcode value.
The Debugger shows the main Thread Suspended and I have to resume the debugger twice before the RuntimeException error appears in Logcat.
here's the error log from LogCat:
07-17 17:38:49.251: E/AndroidRuntime(30942):
java.lang.RuntimeException: Unable to resume activity
{com.fmi.inventory/com.fmi.inventory.MainActivity}:
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=49374, result=-1, data=Intent {
act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }}
to activity {com.fmi.inventory/com.fmi.inventory.MainActivity}:
java.lang.NullPointerException
Here is my MainActivity:
package com.fmi.inventory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton button;
ImageButton button1;
ImageButton button2;
EditText editField;
Activity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
button = (ImageButton)findViewById(R.id.scanCubeID);
button1 = (ImageButton)findViewById(R.id.scanEmployeeID);
button2 = (ImageButton)findViewById(R.id.scanConfigID);
button.setOnClickListener(listener);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onResume() {
super.onResume();
}
public void setCubeClick(){
editField = (EditText)findViewById(R.id.editCubeID);
}
public void setEmployeeClick(){
editField = (EditText)findViewById(R.id.editEmployeeID);
}
public void setConfigClick(){
editField = (EditText)findViewById(R.id.editConfigID);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.initiateScan();
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult = IntentIntegrator
.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
this.editField.setText(contents);
// this.elemQuery.setText(contents);
//this.resume = false;
Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: "
+ format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
}
}
}
Here is my layout .xml for MainActivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editCubeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/scanEmployeeID"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/scanCubeID"
android:ems="10"
android:hint="#string/edit_cubeid" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editEmployeeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/scanConfigID"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/editCubeID"
android:layout_below="#+id/editCubeID"
android:ems="10"
android:hint="#string/edit_employeeid" />
<EditText
android:id="#+id/editConfigID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/scanConfigID"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/editEmployeeID"
android:layout_below="#+id/editEmployeeID"
android:ems="10"
android:hint="#string/edit_configid" />
<ImageButton
android:id="#+id/scanCubeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/desc"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_scan"
android:onClick="onClick" />
<ImageButton
android:id="#+id/scanEmployeeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/desc"
android:layout_below="#+id/scanCubeID"
android:src="#drawable/ic_action_scan"
android:onClick="onClick" />
<ImageButton
android:id="#+id/scanConfigID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/desc"
android:layout_below="#+id/scanEmployeeID"
android:src="#drawable/ic_action_scan"
android:onClick="onClick" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editConfigID"
android:text="#string/button_continue" />
</RelativeLayout>
When your Activity come in onActivityResult after scanning.You are accessing editField but it is NULL.
as you never Initialized it..
You are Initializing them in setCubeClick, setEmployeeClick or setConfigClick,
But I am affraid they never called..Try to Initialize them in OnCreat

Categories

Resources