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
Related
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. :-)
This is my MainActivity.java
I used // to mark as not using on some lines and found that the line start with int t =
Is the problem that make the application to crash and force closing.
package com.example.camera_test;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 1337;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
#SuppressLint("NewApi") #Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, CAMERA_PIC_REQUEST );
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#SuppressLint("NewApi") protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}
I added a button when I click on it it will open the camera on my device after I take a photo it will show the photo I took on a small window. It's working.
Then I added this 3 lines:
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
I wanted to show also the ByteCount of the image on screen in my device.
Once I add the line: int t = thumbnail.getByteCount(); there was an error so I did automatic fix and it added #SuppressLint("NewApi")
Then I marked with // this 3 lines once I unmarked and used the line int t = thumbnail.getByteCount(); so after I took a photo it crashed and told me it need to force close.
Why does it crash on this line ?
This is the file activity_main.xml
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="45dp"
android:layout_marginTop="62dp"
android:text="Activate The Camera" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
I can't figure out why it crash with the int t line.
I added a breakpoint on the int t line but it never stop there and never stop anywhere on my application when I add a breakpoint why ?
The getByteCount() is available from API level 12 (android 3.1+).
Are you sure you are building/running this version or above?
Also please add LogCat in the feature, much easier to find the error.
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
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
}
}
};
This is the java code and xml file for a program I am writing, and it force closes whenever i try to invoke the plusCalc button in-program. Could someone please tell me why?
Thank you!
Java file:
package org.example.knittingframe;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class KnittingFrame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View plusCalc = findViewById(R.id.plus_calc_button);
plusCalc.setOnClickListener(this);
View exitbutton = findViewById(R.id.exit_button);
exitbutton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.plus_calc_button:
startPlusCalc();
break;
case R.id.exit_button:
finish();
break;
}
}
public void startPlusCalc() {
Intent i = new Intent(this, PlusCalc.class);
startActivity(i);
}
}
Here is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="#string/main_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="#+id/plus_calc_button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="#string/plusCalc_label"
android:layout_gravity="center" />
<Button
android:id="#+id/exit_button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="Exit"
android:layout_gravity="center" />
</LinearLayout>
Without the stack trace it appears that the problem is with declaring the PlusCalc.class class in the App Manifest file, because as you say, the error only appears when you click on the PlusCalc button.
If this doesn't help please post the stack trace.