I wrote an android program. This has multiple tab.. I want when I clicked a tab(Tab1), the tab's special view(GalleryDemoActivity.java) was shown.. But the Program has stopped working and does'nt work truly !
The code is:
src/Tab.java:
package com.AdMd.Plant;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Tabs extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.tabs);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
Intent photosIntent = new Intent(this, GalleryDemoActivity.class);
spec1.setContent(photosIntent);
spec1.setIndicator("اطلاعات گیاه");
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("گالری تصاویر");
spec2.setContent(R.id.tab2);
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("گیاهان مشابه");
spec3.setContent(R.id.tab3);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
}
}
The logcat messages are:
07-18 08:32:37.402: D/gralloc_goldfish(1068): Emulator without GPU emulation detected.
07-18 08:32:40.272: D/AndroidRuntime(1068): Shutting down VM
07-18 08:32:40.272: W/dalvikvm(1068): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-18 08:32:40.344: E/AndroidRuntime(1068): FATAL EXCEPTION: main
07-18 08:32:40.344: E/AndroidRuntime(1068): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AdMd.Plant/com.AdMd.Plant.Tabs}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.os.Looper.loop(Looper.java:137)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 08:32:40.344: E/AndroidRuntime(1068): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 08:32:40.344: E/AndroidRuntime(1068): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 08:32:40.344: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 08:32:40.344: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 08:32:40.344: E/AndroidRuntime(1068): at dalvik.system.NativeStart.main(Native Method)
07-18 08:32:40.344: E/AndroidRuntime(1068): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:680)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.widget.TabHost.setCurrentTab(TabHost.java:346)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.widget.TabHost.addTab(TabHost.java:236)
07-18 08:32:40.344: E/AndroidRuntime(1068): at com.AdMd.Plant.Tabs.onCreate(Tabs.java:32)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.Activity.performCreate(Activity.java:4465)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 08:32:40.344: E/AndroidRuntime(1068): ... 11 more
As you see in logcat, it said to me to have a activity Group, But when I use it, the program has some warning and it does'nt work again!
The warning are:
1.The method onCreate(Bundle) from the type ActivityGroup is deprecated
2.The type ActivityGroup is deprecated
What should I've done?
Thanks..
you have to provide action for every tab
just try this code
public class Tabs extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.tabs);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setIndicator("اطلاعات گیاه");
spec1.setContent(new Intent(this,GalleryDemoActivity.class));
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("گالری تصاویر");
spec2.setContent(new Intent(this,GalleryDemoActivity.class));
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("گیاهان مشابه");
spec3.setContent(new Intent(this,GalleryDemoActivity.class));
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
}
}
Related
I have a LinearLayout to which i populated other views using addview(); but when i try to get the height of the Linear layout, it returns 0.
I used ((LinearLayout)findViewById(R.id.linearId)).getHeight();
view i am adding, also has child views. How do i get the height of the Linearlayout including all the children?
EDIT -
actual code i used to adding view to my linear layout -
final ViewTreeObserver viewTreeObserver = earningsContainer.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// read height with myLinearLayout.getHeight() etc.
Log.e(TAG, "$$$$$height "+earningsContainer.getHeight());
// remember to remove the listener if possible
if (viewTreeObserver.isAlive()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
} else {
viewTreeObserver.removeGlobalOnLayoutListener(this);
}
}
}
});
earningsContainer.addView(getRowTaxEarningsTitle());
for (int i = 0; i < taxMainObject.getTaxEarnings().size(); i++) {
earningsContainer.addView(getTaxEarningsRow(taxMainObject
.getTaxEarnings().get(i)));
totalEarnings += taxMainObject.getTaxEarnings().get(i)
.getTotalAmount();
}
earningsContainer.addView(getTaxTotalRow("GROSS TOTAL SALARY",
totalEarnings + ""));
Stacktrace after using ViewTreeObserver -
07-18 13:40:59.936: E/AndroidRuntime(10058): FATAL EXCEPTION: main
07-18 13:40:59.936: E/AndroidRuntime(10058): Process: com.synthesize.paysal, PID: 10058
07-18 13:40:59.936: E/AndroidRuntime(10058): java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.checkIsAlive(ViewTreeObserver.java:720)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.removeOnGlobalLayoutListener(ViewTreeObserver.java:529)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.synthesize.paysal.TaxinfoActivity$1.onGlobalLayout(TaxinfoActivity.java:125)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:815)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1782)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Handler.handleCallback(Handler.java:733)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Looper.loop(Looper.java:136)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 13:40:59.936: E/AndroidRuntime(10058): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 13:40:59.936: E/AndroidRuntime(10058): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 13:40:59.936: E/AndroidRuntime(10058): at dalvik.system.NativeStart.main(Native Method)
After adding a child to your LinearLayout it doesn't perform measure and layout immediately, so at that moment its size is still unknown. You need to wait for for a layout phase to know the dimensions.
You can do it by subclassing your LinearLayout and read it's size in onSizeChanged method etc.
OR you can achieve it without subclassing LinearLayout by setting a OnGlobalLayoutListener on ViewTreeObserver:
myLinearLayout = ((LinearLayout)findViewById(R.id.linearId);
and then add an OnGlobalLayoutListener right before adding your views:
myLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// read height with myLinearLayout.getHeight() etc.
// remember to remove the listener if possible
ViewTreeObserver viewTreeObserver = myLinearLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
} else {
viewTreeObserver.removeGlobalOnLayoutListener(this);
}
}
}
});
mLinearLayout.addView(view);
I am trying to create a custom marker in google maps.I want to add a custom layout as the marker
So i used the following code inside a webservice:
protected void onPostExecute(Void result) {
View custom_layout = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout,null);
ImageView iv_category_logo=(ImageView) custom_layout.findViewById(R.id.iv_category_logo);
pinbit=Bitmap.createScaledBitmap(pinbit,75,56,false);
iv_category_logo.setImageBitmap(pinbit);
//pinbit=MainActivity.getCroppedBitmap(pinbit, 10);
marker.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(getApplicationContext(), custom_layout)));
// adding marker
googleMap.addMarker(marker);
}
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
But a classcastexception is shown in the following line:
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
The logcat is shown below:
07-18 16:08:07.617: E/AndroidRuntime(22114): FATAL EXCEPTION: main
07-18 16:08:07.617: E/AndroidRuntime(22114): java.lang.ClassCastException: android.app.Application
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.igloo.storelocater.MainActivity.createDrawableFromView(MainActivity.java:610)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.igloo.storelocater.MainActivity$retrieveimage.onPostExecute(MainActivity.java:600)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.igloo.storelocater.MainActivity$retrieveimage.onPostExecute(MainActivity.java:1)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.Looper.loop(Looper.java:130)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.app.ActivityThread.main(ActivityThread.java:3689)
07-18 16:08:07.617: E/AndroidRuntime(22114): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:08:07.617: E/AndroidRuntime(22114): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-18 16:08:07.617: E/AndroidRuntime(22114): at dalvik.system.NativeStart.main(Native Method)
}
please help!!
The problem is in using of getApplicationContext() method. It is strongly recommended to avoid using it when you're working with some screen local stuff. See documentation for this method. It does not return an Activity object. I would suggest you to use inter-process communication (IPC) to pass data between your Service and Activity. One possible way is here. For another ways you can read Developers page.
Working on a project with google maps in it.I am trying to retrieve bitmap images from a URL and it works fine ,but not all the time
The code i am using to retrieve the url is inside a async class.
The async class:
public class retrieveimage extends AsyncTask<String,Void,Void>
{
Store_data s;
MarkerOptions marker;
Bitmap pinbit;
public retrieveimage(Store_data s) {
this.s=s;
}
#Override
protected Void doInBackground(String... arg0) {
try {
marker = new MarkerOptions().position(new LatLng(Double.parseDouble(s.store_latitude),Double.parseDouble(s.store_longitude))).title(s.store_name);
String url1=arg0[0];
URL url = new URL(url1);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap= BitmapFactory.decodeStream(input);
pinbit=bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
#Override
protected void onPostExecute(Void result) {
View custom_layout = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout,null);
ImageView iv_category_logo=(ImageView) custom_layout.findViewById(R.id.iv_category_logo);
pinbit=Bitmap.createScaledBitmap(pinbit,68,62,false);
iv_category_logo.setImageBitmap(pinbit);
//pinbit=MainActivity.getCroppedBitmap(pinbit, 10);
marker.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(StoreFinal.this, custom_layout)));
// adding marker
googleMap.addMarker(marker);
}
}
I am calling the async class as:
new retrieveimage(sobj).execute(URL_image+sobj.category+".png");
The problem is that the variable pinbit gets NPE randomly.Many times the desired result is seen.But sometimes out of random i get NPE.So i think there is something wrong with the doInBackground method where the web operation takes place.How do i ensure that the code with the web operation doesnt break?
The LOGCAT:
07-18 18:26:20.289: E/AndroidRuntime(27803): FATAL EXCEPTION: main
07-18 18:26:20.289: E/AndroidRuntime(27803): java.lang.NullPointerException
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:344)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.igloo.storelocater.StoreFinal$retrieveimage.onPostExecute(StoreFinal.java:177)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.igloo.storelocater.StoreFinal$retrieveimage.onPostExecute(StoreFinal.java:1)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.Looper.loop(Looper.java:130)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.app.ActivityThread.main(ActivityThread.java:3689)
07-18 18:26:20.289: E/AndroidRuntime(27803): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 18:26:20.289: E/AndroidRuntime(27803): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-18 18:26:20.289: E/AndroidRuntime(27803): at dalvik.system.NativeStart.main(Native Method)
Just try integrating AQuery library and use its callback method which will give you bitmap along with URL which it belongs. Hope it helps!
When I try to run this line in order to hide the keyboard (I get the InputMethodManager):
this.context = context;
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); //this line crashes the app
What should I do to fix it?
(I am running it from a fragment by the way)
Crash Log:
11-03 16:20:26.700: D/AndroidRuntime(2809): Shutting down VM
11-03 16:20:26.700: W/dalvikvm(2809): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-03 16:20:26.710: E/AndroidRuntime(2809): FATAL EXCEPTION: main
11-03 16:20:26.710: E/AndroidRuntime(2809): java.lang.NullPointerException
11-03 16:20:26.710: E/AndroidRuntime(2809): at co.emuze.tabtest1.Tab1$1.onClick(Tab1.java:32)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View.performClick(View.java:4084)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View$PerformClick.run(View.java:16966)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.handleCallback(Handler.java:615)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Looper.loop(Looper.java:137)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 16:20:26.710: E/AndroidRuntime(2809): at dalvik.system.NativeStart.main(Native Method)
Full on click method:
public void onClick(View v) {
text1.setText(editText1.getText().toString());
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
}
It would appear context is null - you show the line this.context = context, and if the right hand side context is not a variable local to that method you're doing nothing. I think what you may be looking for is context = getApplicationContext()
Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app crashed
User below code in Fragment
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
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.