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);
}
Related
My code is as below but.. when i ran it, it doesn't do anything
Animation animation = new AlphaAnimation(1, 0); // Change alpha
// from fully
// visible to
// invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at
// the
// end so the layout will
// fade back in
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
x.clearAnimation();
logcat after vipul mittal suggest
12-11 20:08:18.477: E/AndroidRuntime(1571): FATAL EXCEPTION: main
12-11 20:08:18.477: E/AndroidRuntime(1571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.NTS.standroid/com.NTS.standroid.Settings}: java.lang.NullPointerException
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.os.Looper.loop(Looper.java:130)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-11 20:08:18.477: E/AndroidRuntime(1571): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 20:08:18.477: E/AndroidRuntime(1571): at java.lang.reflect.Method.invoke(Method.java:507)
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-11 20:08:18.477: E/AndroidRuntime(1571): at dalvik.system.NativeStart.main(Native Method)
12-11 20:08:18.477: E/AndroidRuntime(1571): Caused by: java.lang.NullPointerException
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.NTS.standroid.Settings.onCreate(Settings.java:35)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-11 20:08:18.477: E/AndroidRuntime(1571): ... 11 more
any suggestions please.
I want to make my background to constantly flash after i hit a button
My current code
}
#SuppressLint("NewApi")
public void tintBackground() {
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
ColorDrawable[] color = { new ColorDrawable(Color.RED),
new ColorDrawable(Color.WHITE) };
TransitionDrawable trans = new TransitionDrawable(color);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
x.setBackgroundDrawable(trans);
} else {
x.setBackground(trans);
}
trans.startTransition(2000); // do transition over 2 seconds
}
and xml 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"
android:layout="#+id/warning">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#+id/warning1"
android:layout_marginTop="50dp"
android:layout_marginLeft="100dp"
android:background="#drawable/warning"
/>
</LinearLayout>
I use the following code to tint the background of my app, maybe you can use that repeatedly:
#SuppressLint("NewApi")
public void tintBackground() {
View rootView = findViewById(android.R.id.content);
ColorDrawable[] color = { new ColorDrawable(Color.RED),
new ColorDrawable(Color.WHITE) };
TransitionDrawable trans = new TransitionDrawable(color);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rootView.setBackgroundDrawable(trans);
} else {
rootView.setBackground(trans);
}
trans.startTransition(2000); // do transition over 2 seconds
}
You must start animation on the linearlayout:
Animation animation = new AlphaAnimation(1, 0); // Change alpha
// from fully
// visible to
// invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at
// the
// end so the layout will
// fade back in
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
x.startAnimation(animation)//<-- start animation don't clear it
I have used this to make a View fade in and out repeatedly:
fadeIn = new AlphaAnimation(0.25f, 1);
fadeIn.setInterpolator(new AccelerateInterpolator()); // add this
fadeIn.setDuration(500);
fadeIn.setFillAfter(true);
fadeIn.setAnimationListener(new RepeatAnimationListener());
fadeOut = new AlphaAnimation(1, 0.25f);
fadeOut.setInterpolator(new DecelerateInterpolator()); // and this
fadeOut.setDuration(500);
fadeOut.setFillAfter(true);
fadeOut.setAnimationListener(new RepeatAnimationListener());
private boolean currently_fadeOut;
private class RepeatAnimationListener implements AnimationListener {
public void onAnimationEnd(Animation animation) {
if (currently_fadeOut) {
view.startAnimation(fadeIn);
currently_fadeOut = false;
} else {
view.startAnimation(fadeOut);
currently_fadeOut = true;
}
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
}
// automatically repeated because of RepeatAnimationListener
view.startAnimation(fadeOut);
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
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;
}
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();
/***/
}
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.