FATAL EXCEPTION: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException - java

I see this error popping almost a million times and people have got the help. As a beginner, I would like to seek some help for the simple media recorder that I'm developing. Please help.
Below mentioned is the code-
add_company.java
public class add_company extends Activity{
//Adding for voice record
ImageButton play,record,stop;
private String s,voicerec;
MediaPlayer mp;
MediaRecorder mr;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.add_company);
//Adding voice record
play = (ImageButton)findViewById(R.id.Play);
record = (ImageButton)findViewById(R.id.Record);
stop = (ImageButton)findViewById(R.id.Stop);
File audioFile = null;
try {
audioFile = File.createTempFile(null, ".amr");
} catch (IOException e1) {
e1.printStackTrace();
}
mr = new MediaRecorder();
s = audioFile.getAbsolutePath();
voicerec=s;
System.out.println("Path: "+s);
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mr.setOutputFile(s);
try{
mr.prepare();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void record(View v){
Toast.makeText(add_company.this, "Recording", Toast.LENGTH_LONG).show();
mr.start();
}
public void stop(View v){
mr.stop(); //this is stop recording
Toast.makeText(add_company.this, "Stopped", Toast.LENGTH_LONG).show();
mr.reset(); // this is required to avoid the error: Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1)
mr.release();
}
public void play(View v){
File f = new File(voicerec);
Uri u = Uri.fromFile(f);
mp = MediaPlayer.create(add_company.this, u);
mp.setLooping(false);
mp.start();
}
}
LogCat details:
06-18 10:31:53.281: E/AndroidRuntime(2740): FATAL EXCEPTION: main
06-18 10:31:53.281: E/AndroidRuntime(2740): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tg_db1/com.example.tg_db1.add_company}: java.lang.NullPointerException
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.os.Looper.loop(Looper.java:137)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 10:31:53.281: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 10:31:53.281: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 10:31:53.281: E/AndroidRuntime(2740): at dalvik.system.NativeStart.main(Native Method)
06-18 10:31:53.281: E/AndroidRuntime(2740): Caused by: java.lang.NullPointerException
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.io.File.createTempFile(File.java:999)
06-18 10:31:53.281: E/AndroidRuntime(2740): at java.io.File.createTempFile(File.java:970)
06-18 10:31:53.281: E/AndroidRuntime(2740): at com.example.tg_db1.add_company.onCreate(add_company.java:64)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.Activity.performCreate(Activity.java:5104)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-18 10:31:53.281: E/AndroidRuntime(2740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-18 10:31:53.281: E/AndroidRuntime(2740): ... 11 more

File.createTempFile(null, ".amr");
Prefix can't be null.
From the source code :
public static File createTempFile(String prefix, String suffix, File directory) throws IOException
{
if (prefix == null) throw new NullPointerException();
/***/
}

Related

How to get ImageView Size in Android?

Im trying to get imageview size but its returning zero. i know its because im calling it in onCreate(). can anyone help me how to do this so that when I will click a button it will show me the size of that ImageView. here what i have tried so far...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
imgView = (ImageView)findViewById(R.id.imgView);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getSize();
}
});
}
void getSize)
{
BitmapDrawable bd = (BitmapDrawable)imgView.getDrawable();
bmp = bd.getBitmap();
Bitmap op = Bitmap.createBitmap(bmp, imgWidth/2, imgHeight/2, imgWidth, imgHeight);
imgView.setImageBitmap(op);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
imgWidth=imgView.getWidth();
imgHeight=imgView.getHeight();
}
UPDATE
This is what I am getting right now...
06-18 00:13:20.206: E/AndroidRuntime(919): FATAL EXCEPTION: main
06-18 00:13:20.206: E/AndroidRuntime(919): java.lang.IllegalArgumentException: x must be >= 0
06-18 00:13:20.206: E/AndroidRuntime(919): at android.graphics.Bitmap.checkXYSign(Bitmap.java:225)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.graphics.Bitmap.createBitmap(Bitmap.java:495)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.graphics.Bitmap.createBitmap(Bitmap.java:471)
06-18 00:13:20.206: E/AndroidRuntime(919): at com.example.bitmapfun.Main.Gray(Main.java:44)
06-18 00:13:20.206: E/AndroidRuntime(919): at com.example.bitmapfun.Main$1.onClick(Main.java:34)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.view.View.performClick(View.java:3527)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.view.View$PerformClick.run(View.java:14234)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.os.Handler.handleCallback(Handler.java:605)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.os.Looper.loop(Looper.java:137)
06-18 00:13:20.206: E/AndroidRuntime(919): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-18 00:13:20.206: E/AndroidRuntime(919): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 00:13:20.206: E/AndroidRuntime(919): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 00:13:20.206: E/AndroidRuntime(919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-18 00:13:20.206: E/AndroidRuntime(919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-18 00:13:20.206: E/AndroidRuntime(919): at dalvik.system.NativeStart.main(Native Method)
Try this it uses view tree observer for getting the width and height. The earlier methods didnot work because the view was not drawn in the screen(layout pass incomplete). Something similar should work:
final ImageView iv = (ImageView)findViewById(R.id.image);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("width",""+iv.getWidth());
}
});
The size isn't set up until the component is added to the parent window, and then the window is "packed" or laid out to the right size. So in the onCreate, it will be 0,0
Can't you just get the size when the function is called?
void getSize()
{
imgWidth=imgView.getWidth();
imgHeight=imgView.getHeight();
BitmapDrawable bd = (BitmapDrawable)imgView.getDrawable();
bmp = bd.getBitmap();
Bitmap op = Bitmap.createBitmap(bmp, imgWidth/2, imgHeight/2, imgWidth, imgHeight);
imgView.setImageBitmap(op);
}

Spinner problems with Sharedpreferences

Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.

Android - Make Custom Alert Dialogue disappear after clicked

I am trying to make it so that when a button is pressed an alertdialogue pops up with a spinner in it, and in the spinner there are three options, how can i make it so that when one of the options are selected the alertdialogue disappears, my code below crashed whenever I select something from the spinner
Below is my code
public void AlertD() {
LayoutInflater li = LayoutInflater.from(getActivity());
View promptsView = li.inflate(R.layout.abs, null);
// above I am setting the customview of the alert dialogue
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());
alertDialogBuilder.setView(promptsView);
// here I am officially setting the custom layout
// set dialog message
alertDialogBuilder.setTitle(dialoguechoice + " Stretch Type");
// alert dialogue title
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
// above is creating the alert dialogue
final Spinner mSpinner = (Spinner) promptsView
.findViewById(R.id.sSType);
// above is initializing the spinner
// reference UI elements from my_dialog_layout in similar fashion
mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
// show it
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(true);
}
Below is my listener
public class OnSpinnerItemClicked implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch (pos) {
case (1):
alertDialog.dismiss();
break;
}
}
#Override
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
EDIT logcat is below
06-18 18:09:55.939: E/AndroidRuntime(4355): FATAL EXCEPTION: main
06-18 18:09:55.939: E/AndroidRuntime(4355): java.lang.NullPointerException
06-18 18:09:55.939: E/AndroidRuntime(4355): at com.OptimusApps.stayhealthy.Body$OnSpinnerItemClicked.onItemSelected(Body.java:178)
06-18 18:09:55.939: E/AndroidRuntime(4355): at android.widget.AdapterView.fireOnSelected(AdapterView.java:882)
06-18 18:09:55.939: E/AndroidRuntime(4355): at android.widget.AdapterView.access$200(AdapterView.java:48)
06-18 18:09:55.939: E/AndroidRuntime(4355): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:848)
06-18 18:09:55.939: E/AndroidRuntime(4355): at android.os.Handler.handleCallback(Handler.java:605)
06-18 18:09:55.939: E/AndroidRuntime(4355): at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 18:09:55.939: E/AndroidRuntime(4355): at android.os.Looper.loop(Looper.java:137)
06-18 18:09:55.939: E/AndroidRuntime(4355): at android.app.ActivityThread.main(ActivityThread.java:4575)
06-18 18:09:55.939: E/AndroidRuntime(4355): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 18:09:55.939: E/AndroidRuntime(4355): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 18:09:55.939: E/AndroidRuntime(4355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-18 18:09:55.939: E/AndroidRuntime(4355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-18 18:09:55.939: E/AndroidRuntime(4355): at dalvik.system.NativeStart.main(Native Method)
Thanks

Simple calculator crashes because of null pointer

I've just started to look at Android development and I followed a tutorial on how to make a super simple basic calculator (with just sum, sub, div and mult operations) but my APP is full of bugs and one of them is crashing when I do an operation without any inserted values.
This is just a portion of the code and I think the crash is in this part (for every button). How can I solve this issue without crashing the app? Like doing nothing when one TextEdit is blank? Do I need any throw/catch exception?
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firstNum = new BigDecimal(firstNumber.getText().toString());
secondNum = new BigDecimal(secNumber.getText().toString());
if (firstNum == null || secondNum == null) {
while (verificar) {
}
} else {
result.setText(firstNum.subtract(secondNum).toString());
}
}
});
The logcat.txt of the errors...
Thanks for your patience...
06-18 16:39:51.018: E/AndroidRuntime(621): FATAL EXCEPTION: main
06-18 16:39:51.018: E/AndroidRuntime(621): java.lang.NumberFormatException: Bad offset/length: offset=0 len=0 in.length=0
06-18 16:39:51.018: E/AndroidRuntime(621): at java.math.BigDecimal.<init>(BigDecimal.java:282)
06-18 16:39:51.018: E/AndroidRuntime(621): at java.math.BigDecimal.<init>(BigDecimal.java:438)
06-18 16:39:51.018: E/AndroidRuntime(621): at com.example.simplecalculator.Calculadora$1.onClick(Calculadora.java:44)
06-18 16:39:51.018: E/AndroidRuntime(621): at android.view.View.performClick(View.java:4084)
06-18 16:39:51.018: E/AndroidRuntime(621): at android.view.View$PerformClick.run(View.java:16966)
06-18 16:39:51.018: E/AndroidRuntime(621): at android.os.Handler.handleCallback(Handler.java:615)
06-18 16:39:51.018: E/AndroidRuntime(621): at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 16:39:51.018: E/AndroidRuntime(621): at android.os.Looper.loop(Looper.java:137)
06-18 16:39:51.018: E/AndroidRuntime(621): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-18 16:39:51.018: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 16:39:51.018: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 16:39:51.018: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-18 16:39:51.018: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-18 16:39:51.018: E/AndroidRuntime(621): at dalvik.system.NativeStart.main(Native Method)
06-18 16:41:07.109: E/Trace(644): error opening trace file: No such file or directory (2)
06-18 16:41:09.339: E/AndroidRuntime(644): FATAL EXCEPTION: main
06-18 16:41:09.339: E/AndroidRuntime(644): java.lang.NumberFormatException: Bad offset/length: offset=0 len=0 in.length=0
06-18 16:41:09.339: E/AndroidRuntime(644): at java.math.BigDecimal.<init>(BigDecimal.java:282)
06-18 16:41:09.339: E/AndroidRuntime(644): at java.math.BigDecimal.<init>(BigDecimal.java:438)
06-18 16:41:09.339: E/AndroidRuntime(644): at com.example.simplecalculator.Calculadora$1.onClick(Calculadora.java:44)
06-18 16:41:09.339: E/AndroidRuntime(644): at android.view.View.performClick(View.java:4084)
06-18 16:41:09.339: E/AndroidRuntime(644): at android.view.View$PerformClick.run(View.java:16966)
06-18 16:41:09.339: E/AndroidRuntime(644): at android.os.Handler.handleCallback(Handler.java:615)
06-18 16:41:09.339: E/AndroidRuntime(644): at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 16:41:09.339: E/AndroidRuntime(644): at android.os.Looper.loop(Looper.java:137)
06-18 16:41:09.339: E/AndroidRuntime(644): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-18 16:41:09.339: E/AndroidRuntime(644): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 16:41:09.339: E/AndroidRuntime(644): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 16:41:09.339: E/AndroidRuntime(644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-18 16:41:09.339: E/AndroidRuntime(644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-18 16:41:09.339: E/AndroidRuntime(644): at dalvik.system.NativeStart.main(Native Method)
The assignment of the BigDecimal will throw a NumberFormatException if the string is empty. Change this:
firstNum = new BigDecimal(firstNumber.getText().toString());
secondNum = new BigDecimal(secNumber.getText().toString());
to this:
try {
firstNum = new BigDecimal(firstNumber.getText().toString());
} catch (NumberFormatException e) {
firstNum = null;
}
try {
secondNum = new BigDecimal(secNumber.getText().toString());
} catch (NumberFormatException e) {
secondNum = null;
}

using startActivityForResult

i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.

Categories