How to use Bottom tabActivity in my Activity itself - java

I want to use bottom tab in my activity,in my old activity itself having some function,without using seperate activity for tab,can i use tab functions in my activity itself,I tried like this.
public class GinfyActivity extends Activity {
/** Called when the activity is first created. */
public GridviewAdapter mAdapter;
private Spinner spinner1;
public String selectprayer;
ArrayList<GridviewAdapter> results = new ArrayList<GridviewAdapter>();
private ArrayList<String> listginfy;
private ArrayList<Integer> listimage;
private GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_ginfy);
TabHost mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(new Intent(GinfyActivity.this,Audioprayer.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(new Intent(GinfyActivity.this,TTSMeditation.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(new Intent(GinfyActivity.this,TTSList.class)));
mTabHost.setCurrentTab(0);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new GridviewAdapter(this,listginfy, listimage);
// Set custom adapter to gridview
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (selectprayer.equals("www.ginfy.com"))
{
switch(position)
{
case 0:
Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
startActivity(newActivity);
break;
case 2:
Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
startActivity(new1Activity);
break;
default:
Toast.makeText(GinfyActivity.this, "Coming Soon", Toast.LENGTH_LONG).show();
}
}
else
{
switch(position)
{
case 0:
Intent new2Activity = new Intent(GinfyActivity.this,YourPrayerActivity.class);
startActivity(new2Activity);
break;
case 1:
Intent new3Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity1.class);
startActivity(new3Activity);
break;
case 3:
Intent new4Activity = new Intent(GinfyActivity.this,ExperiencesActivity.class);
startActivity(new4Activity);
break;
default:
Toast.makeText(GinfyActivity.this, "Coming Soon", Toast.LENGTH_LONG).show();
}
}
}
});
}
public void addListenerOnSpinnerItemSelection() {
{
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
spinner1.setSelection(position);
//spinnerCapital.setSelection(position);
selectprayer = (String) spinner1.getSelectedItem();
//Toast.makeText(getBaseContext(), selectprayer, Toast.LENGTH_SHORT).show();
//String myStatesCapital = (String) spinnerCapital.getSelectedItem();
//tvCapital.setText("My State is " + myState + ".And its Capital is "
//+ myStatesCapital);
}
public void onNothingSelected(AdapterView<?> parent) {
}});
}
}
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
}
public void prepareList()
{
listginfy = new ArrayList<String>();
listginfy.add("Prayers");
listginfy.add("Prayer room");
listginfy.add("God gallery");
listginfy.add("Experiences");
listimage = new ArrayList<Integer>();
listimage.add(R.drawable.ginfyprayer);
listimage.add(R.drawable.poojaroom1);
listimage.add(R.drawable.godsgallery);
listimage.add(R.drawable.temp1);
}
}
I declared that tab functions in mylayout also but my activity is not opening,it display my activity has stopped
is showing somelogcat error.
09-12 08:43:45.615: E/AndroidRuntime(2630): FATAL EXCEPTION: main
09-12 08:43:45.615: E/AndroidRuntime(2630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonandroid/com.example.jsonandroid.GinfyActivity}: java.lang.NullPointerException
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.os.Looper.loop(Looper.java:137)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.ActivityThread.main(ActivityThread.java:5039)
09-12 08:43:45.615: E/AndroidRuntime(2630): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 08:43:45.615: E/AndroidRuntime(2630): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 08:43:45.615: E/AndroidRuntime(2630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-12 08:43:45.615: E/AndroidRuntime(2630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-12 08:43:45.615: E/AndroidRuntime(2630): at dalvik.system.NativeStart.main(Native Method)
09-12 08:43:45.615: E/AndroidRuntime(2630): Caused by: java.lang.NullPointerException
09-12 08:43:45.615: E/AndroidRuntime(2630): at com.example.jsonandroid.GinfyActivity.onCreate(GinfyActivity.java:44)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.Activity.performCreate(Activity.java:5104)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-12 08:43:45.615: E/AndroidRuntime(2630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-12 08:43:45.615: E/AndroidRuntime(2630): ... 11 more

replace these two lines
public class GinfyActivity extends Activity
TabHost mTabHost = (TabHost)findViewById(android.R.id.tabhost);
with following
public class GinfyActivity extends TabActivity
TabHost mTabHost = getTabHost();
EDIT
change activity which you want to display in following line.
Intent intentAndroid = new Intent().setClass(this, Your_Activity.class);
And if you want only Tab then it's better to use ActionBar as TabHost is deprecated.
Full Example of TabHost
To use ActionBar here is sample code (use Sherlock or appcompat Library for < API 11)
public class MainActivity extends Activity implements TabListener {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("TAB 1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("TAB 2").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("TAB 3").setTabListener(this));
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:
//start activity here or toast
Toast.makeText(getApplicationContext(), "Start First Activity", Toast.LENGTH_SHORT).show();
break;
case 1:
//start activity here or toast
Toast.makeText(getApplicationContext(), "Start Second Activity", Toast.LENGTH_SHORT).show();
break;
case 2:
//start activity here or toast
Toast.makeText(getApplicationContext(), "Start Third Activity", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}

For your your complete requirement https://github.com/AdilSoomro/Iphone-Tab-in-Android

Related

App crashed when arrayAdapter.notifyDataSetChanged() was called

public class Activity_search extends ActionBarActivity {
private BluetoothAdapter mBtAdapter;
private ArrayAdapter<String> mArrayAdapter;
private ArrayList<String> mArrayList;
private boolean isDiscovering;
private AdapterView.OnItemClickListener mClickListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> adapterView,View view,int position, long id ){
mBtAdapter.cancelDiscovery();
isDiscovering=false;
Toast.makeText(getApplicationContext(),"Selected",Toast.LENGTH_SHORT).show();
String tmp1 = ((TextView)view).getText().toString();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_search);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
mArrayList = new ArrayList<String>();
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,R.layout.listview_row,mArrayList);
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(mArrayAdapter);
listView.setOnItemClickListener(mClickListener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_search, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String tmp=device.getName() + "\n" + device.getAddress();
Context appContext = getApplicationContext();
Toast.makeText(appContext,tmp,Toast.LENGTH_LONG).show();
mArrayList.add(tmp);
mArrayAdapter.notifyDataSetChanged();
}
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
Toast.makeText(getApplicationContext(),"Discovery Finished",Toast.LENGTH_SHORT).show();
}
}
};
public void onBtn2Clicked(View view) {
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Context appContext = getApplicationContext();
if(!isDiscovering) {
mBtAdapter.startDiscovery();
Toast.makeText(appContext, "Discovery Started", Toast.LENGTH_SHORT).show();
isDiscovering=true;
} else {
mBtAdapter.cancelDiscovery();
Toast.makeText(appContext,"Discovery Canceled",Toast.LENGTH_SHORT).show();
isDiscovering=false;
}
}
public void onDestroy(){
super.onDestroy();
if(mBtAdapter.isDiscovering()){
mBtAdapter.cancelDiscovery();
}
unregisterReceiver(mReceiver);
}
}
App always crashed when it called the function as mentioned at above, And its error code is
02-13 16:04:47.974 27328-27328/com.skyjohn.nxtrpc E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in com.skyjohn.nxtrpc.Activity_search$2#41532570
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:798)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.skyjohn.nxtrpc.Activity_search$2.onReceive(Activity_search.java:90)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:788)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
But if i hardcoded mArrayList.add("abc") into the code, the "abc" will appear, but if I do it dynamicly (when I received a new bluetooth device) , the app crashed.
Caused by: java.lang.NullPointerException
at com.skyjohn.nxtrpc.Activity_search$2.onReceive
mArrayAdapter is null because creating new object of Adapter in onCreate method with same name which declare as class level.
To fix error use:
mArrayAdapter = new ArrayAdapter<String>(this,R.layout.listview_row,mArrayList);
in onCreate method instead of creating new object of ArrayAdapter.
inside onCreate you are hiding the scope of the class member variable mAdapter, declaring and initialazing it in the local scope of onCreate. The fix is to remove ArrayAdapter<String> from onCreate, leaving only
mArrayAdapter = new ArrayAdapter<String>(this,R.layout.listview_row,mArrayList);

Updating Listview ( SharedPreferences -> ArrayList) from separate Class

I would like to update my Listview in a fragmen in a onPostExecute() separate class.
The first initialization of the the Listview doas work, but wehe I call createList() again, the App crashes (NullPointerException)
Any Idea?
Main_Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View fragmentView = inflater.inflate(R.layout.main_fragment, container, false);
StartSocketService = (Button) fragmentView.findViewById(R.id.start_socketservice);
StartSocketService.setOnClickListener(this);
StopSocketService = (Button) fragmentView.findViewById(R.id.stop_socketservice);
StopSocketService.setOnClickListener(this);
listview = (ListView) fragmentView.findViewById(R.id.listView1);
createList();
return fragmentView;
}
public void createList(){
//Reading Server IPs from SharedPreferences and put them to ListView
SharedPreferences settings = getActivity().getSharedPreferences("Found_Devices", 0);
for (int i = 0; i < 255; i++) {
if ((settings.getString("Server"+i,null)) != null) {
serverList.add(settings.getString("Server"+i, null));
Log.v("Reading IP: " +settings.getString("Server"+i, null), " from SraredPreferrences at pos.: "+i );
}
}
//Initializing listView
final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, serverList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0).withEndAction(new Runnable() {
#Override
public void run() {
serverList.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
Some class:
async_cient = new AsyncTask<Void, Void, Void>() {
...
protected void onPostExecute(Void result) {
Toast.makeText(mContext, "Scan Finished", Toast.LENGTH_SHORT).show();
Main_Fragment cList = new Main_Fragment();
cList.createList();
super.onPostExecute(result);
}
};
Log:
07-29 14:42:46.428 3382-3382/de.liquidbeam.LED.control D/AndroidRuntime﹕ Shutting down VM
07-29 14:42:46.428 3382-3382/de.liquidbeam.LED.control W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41549ba8)
07-29 14:42:46.438 3382-3382/de.liquidbeam.LED.control E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.liquidbeam.LED.control, PID: 3382
java.lang.NullPointerException
at de.liquidbeam.LED.control.fragments.Main_Fragment.createList(Main_Fragment.java:56)
at de.liquidbeam.LED.control.background.UDP_Discover$1.onPostExecute(UDP_Discover.java:94)
at de.liquidbeam.LED.control.background.UDP_Discover$1.onPostExecute(UDP_Discover.java:57)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
07-29 14:47:46.591 3382-3382/de.liquidbeam.LED.control I/Process﹕ Sending signal. PID: 3382 SIG: 9
Lines:
56 : SharedPreferences settings = getActivity().getSharedPreferences("Found_Devices", 0);
94 : cList.createList();
57: async_cient = new AsyncTask<Void, Void, Void>() {
You creating a new instance of your fragment with no context (activity) to run in. So
the line
SharedPreferences settings = getActivity().getSharedPreferences("Found_Devices", 0);
Tries to get his activity but there is no activity where the fragment lives in ;)

Android using One Spinner with Two Adapters

I have one spinner, two adapters and two buttons
when first button is clicked the maleAdapter is called
when the other button is clicked the femaleAdapter is called.
So far this works perfect.
Now. my problem is when each adapter is called two different arrays are called, obviously, BUT i want them to do different things.
Spinner spinner;
String[] maleItems = { "item 1", "item 2", "item 3" };
String[] femaleItems = { "item 3", "item 2", "item 1", "item 0" };
Button maleButton;
Button femaleButton;
ArrayAdapter<String> maleAdapter;
ArrayAdapter<String> femaleAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayAdapter<String> maleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, maleItems);
final ArrayAdapter<String> femaleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, femaleItems);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(maleAdapter);
spinner.setOnItemSelectedListener(this);
// Buttons
maleButton = (Button) findViewById(R.id.maleButton);
femaleButton = (Button) findViewById(R.id.femaleButton);
maleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
spinner.setAdapter(maleAdapter);
}
});
femaleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
spinner.setAdapter(femaleAdapter);
}
});
}
Here on my onItemSelected i want to change what happens depending on which button is pressed.
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position = spinner.getSelectedItemPosition();
SpinnerAdapter adap = spinner.getAdapter();
if (adap.equals(maleAdapter)) {
switch (position) {
case 0:
// do something only the maleAdapter does
break;
case 1:
// do something only the maleAdapter does
break;
case 2:
// do something only the maleAdapter does
break;
}
} else if (adap.equals(femaleAdapter)) {
switch (position) {
case 0:
// do something only the femaleAdapter does
break;
case 1:
// do something only the femaleAdapter does
break;
case 2:
// do something only the femaleAdapter does
break;
}
}
}
My onClick method worked perfect but the adapter changing gives me an error and i dont know why.
Error
01-25 04:11:18.873: E/AndroidRuntime(21871): FATAL EXCEPTION: main
01-25 04:11:18.873: E/AndroidRuntime(21871): java.lang.NullPointerException
01-25 04:11:18.873: E/AndroidRuntime(21871): at com.MainActivity.onItemSelected(MainActivity.java:117)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.widget.AdapterView.access$200(AdapterView.java:42)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.os.Handler.handleCallback(Handler.java:587)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.os.Looper.loop(Looper.java:130)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.app.ActivityThread.main(ActivityThread.java:3691)
01-25 04:11:18.873: E/AndroidRuntime(21871): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 04:11:18.873: E/AndroidRuntime(21871): at java.lang.reflect.Method.invoke(Method.java:507)
01-25 04:11:18.873: E/AndroidRuntime(21871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
01-25 04:11:18.873: E/AndroidRuntime(21871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
01-25 04:11:18.873: E/AndroidRuntime(21871): at dalvik.system.NativeStart.main(Native Method)
final ArrayAdapter<String> maleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, maleItems);
it is in the local scope of onCreate remove it and initilaize the global maleAdpter over there
just remove final ArrayAdapter before maleAdapter and femaleAdapter.
I'd recommend keeping two separate OnItemSelectedListeners, one for each adapter. Now, when you swap the adapters, also swap the OnItemSelectedListener. Plain and simple.

How to get ListView data from one activity to another using setOnClickListener

In my listview there are to feilds,ward number and the date.i want to get these data to new activity when i'am clicking a listview.but in this there is a runtime error and i have no idea to how to fix it.
this is my dashboardActivity.java file,
public class DashboardActivity extends ListActivity implements FetchDataListener{
UserFunctions userFunctions;
Button btnLogout;
TextView resultView;
private ProgressDialog dialog;
//private String email;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Dashboard Screen for the application
* */
// Check login status in database
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
setContentView(R.layout.dashboard);
initView();
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
TextView txtviewWard= (TextView) view.findViewById(R.id.ward_number);
String ward = txtviewWard.getText().toString();
TextView txtviewDate= (TextView) view.findViewById(R.id.date_time);
String dateTime = txtviewDate.getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("ward", ward);
i.putExtra("dateTime", dateTime);
startActivity(i);
}
});
btnLogout = (Button) findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
});
}else{
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
}
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
Intent intent = getIntent();
String email = intent.getStringExtra(LoginActivity.EMAIL);
String url = "http://pubbapp.comze.com/pubapp1.php?email=" + email;
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
this is my SingleListItem.java file,
public class SingleListItem extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
TextView txtWard = (TextView) findViewById(R.id.ward_number);
TextView txtDate = (TextView) findViewById(R.id.date_time);
Intent i = getIntent();
// getting attached intent data
String wardNumber = i.getStringExtra("ward");
String dateTime = i.getStringExtra("dateTime");
// displaying selected product name
txtWard.setText(wardNumber);
txtDate.setText(dateTime);
}
this is my 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" >
<TextView
android:id="#+id/ward_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="wardNumber"
android:textColor="#ffffff"
android:textSize="25dip"
android:textStyle="bold" />
<TextView
android:id="#+id/date_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Date"
android:padding="10dip"
android:textColor="#ffffff"
android:textSize="20dip"
android:textStyle="bold" />
</LinearLayout>
and this is the error in the LogCat,
12-02 19:10:01.100: E/AndroidRuntime(22705): FATAL EXCEPTION: main
12-02 19:10:01.100: E/AndroidRuntime(22705): java.lang.NullPointerException
12-02 19:10:01.100: E/AndroidRuntime(22705): at com.example.androidhive.DashboardActivity$1.onItemClick(DashboardActivity.java:59)
12-02 19:10:01.100: E/AndroidRuntime(22705): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-02 19:10:01.100: E/AndroidRuntime(22705): at android.widget.ListView.performItemClick(ListView.java:3745)
12-02 19:10:01.100: E/AndroidRuntime(22705): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1980)
12-02 19:10:01.100: E/AndroidRuntime(22705): at android.os.Handler.handleCallback(Handler.java:587)
12-02 19:10:01.100: E/AndroidRuntime(22705): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 19:10:01.100: E/AndroidRuntime(22705): at android.os.Looper.loop(Looper.java:130)
12-02 19:10:01.100: E/AndroidRuntime(22705): at android.app.ActivityThread.main(ActivityThread.java:3691)
12-02 19:10:01.100: E/AndroidRuntime(22705): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 19:10:01.100: E/AndroidRuntime(22705): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 19:10:01.100: E/AndroidRuntime(22705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
12-02 19:10:01.100: E/AndroidRuntime(22705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
12-02 19:10:01.100: E/AndroidRuntime(22705): at dalvik.system.NativeStart.main(Native Method)
please help me to fix this error.
thank you.
In first activity, pass the data as a bundle using putExtras:
// sending data to new activity
Bundle b = new Bundle();
b.putString("ward", ward);
b.putString("dateTime", dateTime);
i.putExtras(b);
In second activity, retrieve the bundle and parse the data as follows:
Bundle b = getIntent().getExtras();
if (b != null)
{
String wardNumber = b.getString("ward");
String dateTime = b.getString("dateTime");
}
try this....
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//your code
}
});
get data like this in ur second activity:
Bundle b = savedInstanceState.getExtras();
if(b!=null)
{
String ward =b.getString("ward");
String dateTime =b.getString("datetime");
}
bundle is responsible to getexras(),so use this.it worked for me and my other friends.

BadTokenException even after referring to Activity and not the application context

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy#406a6678 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:528)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at android.app.Activity.showDialog(Activity.java:2569)
at android.app.Activity.showDialog(Activity.java:2527)
at MyCode$8$4.run(MyCode.java:557)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
I am getting above exception when following code is executed. This file dialog will shown once the processing is done and progressbar reaches 100%. FileSaveDialog extends Dialog and implements OnCompletionListener
runOnUiThread(new Runnable() {
#Override
public void run() {
showDialog(error.Code());//Line 557
}
});
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
final ScrollView scrollView = new ScrollView(this);
final TextView textView = new TextView(this);
switch (id) {
// Other cases are here
case 4:
File playFile = new File(mediaPath, TEMP_WAV_FILE_NAME);
dialog = new FileSaveDialog(this, getResources(),
playFile.getAbsolutePath(), saveDiscardHandler);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// do whatever you want the back key to do
cleanUp();
}
});
break;
// Other cases are here
default:
dialog = null;
}
return dialog;
}
You must check activity isFinishing() If the activity is finishing, returns true; else returns false.

Categories