I have the following problem. I have implemented an app that has to connect to an online database and I wanna inheritance a client code to a variable so I can use it from one activity of the user interface to all others where I need it so I can make my SQL queries. My problem is that the new activity inheritance a null value.
public class MainActivity extends AppCompatActivity {
----->FIRST CLASS<--------
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
String a="";
background b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("PROFIL");
// fab.setVisibility(View.VISIBLE);
toolbar.setTitleTextColor(Color.WHITE);
b = new background(getApplicationContext());
a = b.cod_client;
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_LONG).show();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new profil()).commit();
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
toolbar.setTitleTextColor(Color.WHITE);
if (menuItem.getItemId() == R.id.sport) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
toolbar.setTitle("SPORT");
toolbar.setTitleTextColor(Color.WHITE);
// fab.setVisibility(View.INVISIBLE);
}
else if (menuItem.getItemId() == R.id.acvatice)
{
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment2()).commit();
toolbar.setTitle("ACVATICE");
// fab.setVisibility(View.INVISIBLE);
}
else if(menuItem.getItemId() == R.id.relaxare)
{
toolbar.setTitle("RELAX");
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment3()).commit();
// fab.setVisibility(View.INVISIBLE);
}
else if(menuItem.getItemId() == R.id.masaj)
{
toolbar.setTitle("M");
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment4()).commit();
// fab.setVisibility(View.INVISIBLE);
}
else if(menuItem.getItemId() == R.id.setari)
{
toolbar.setTitle("SETTINGS");
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment5()).commit();
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
toolbar.inflateMenu(R.menu.menu_main);
final ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menu ){
if(menu.getItemId() == R.id.profil)
{
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new profil()).commit();
toolbar.setTitle("PROFIL");
// fab.setVisibility(View.VISIBLE);
}
else if(menu.getItemId() == R.id.rezervare)
{
toolbar.setTitle("REZERVARE");
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new RezervareServicii()).commit();
// fab.setVisibility(View.VISIBLE);
}
else if (menu.getItemId() == R.id.factura)
{
toolbar.setTitle("FACTURA");
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new Factura()).commit();
// fab.setVisibility(View.VISIBLE);
}
return false;
}
});
}
public class TabFragment extends Fragment {
----->SECOND CLASS<--------
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now, this is a workaround,
* The setupWithViewPager doesn't work without the runnable.
* Maybe a Support Library Bug.
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new Fitness();
case 1 : return new Aerobic();
case 2 : return new Yoga();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Fitness";
case 1 :
return "Aerobic";
case 2 :
return "YOGA";
}
return null;
}
}
}
public class background extends AsyncTask<String,String,String> {
Context ctx;
String data = "";
String cnp = "",kg=null,inaltime= null,cod_client;
background(Context context)
{
this.ctx=context.getApplicationContext();
cod_client ="";
}
background(String c)
{
c=cod_client;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String name = params[1];
String password = params[2];
int tmp;
if (type.equals("login")) {
try
{
URL url = new URL("http://192.168.1.1/date_2.php");
String urlParams = "email=" + name + "&parola=" + password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while ((tmp = is.read()) != -1) {
data += (char) tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: " + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exceptionn: " + e.getMessage();
}
}
else if (type.equals("inregistrare"))
{
try {
String numee= params[1];
String prenumee=params[2];
String cnp = params[3];
String telefon =params[4];
String email = params[5];
String parola = params[6];
URL url = new URL("http://192.168.1.1/date.php");
String urlParams = "nume=" + numee+"&prenume="+prenumee+"&cnp="+cnp+"&telefon="+telefon+"&email="+email+"&parola="+parola;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while ((tmp = is.read()) != -1) {
data += (char) tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: " + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exceptionn: " + e.getMessage();
}
}
return null;
}
@Override
protected void onPreExecute()
{
}
@Override
protected void onPostExecute(String result) {
if(result.equals("inregistrare cu succes"))
{
Toast.makeText(ctx,"Inregistrare cu success!",Toast.LENGTH_LONG).show();
}
else if(result.equals("fara success"))
{
Toast.makeText(ctx,"Inregistrare fara success",Toast.LENGTH_LONG).show();
}
String err=null;
try
{
JSONObject emp=(new JSONObject(result)).getJSONObject("user_data");
cod_client = emp.getString("cod_client");
cnp = emp.getString("cnp");
kg=emp.getString("kilograme");
inaltime=emp.getString("inaltime");
Toast.makeText(ctx,"id este "+cod_client+"cnp este "+cnp,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
err = "Exception: "+e.getMessage();
}
if(cod_client!= null && cnp!=null)
{
Toast.makeText(ctx,"Autentificare cu success!",Toast.LENGTH_LONG).show();
Intent intent = new Intent(ctx,MainActivity.class);
intent.putExtra("cod_client", cod_client);
intent.putExtra("cnp", cnp);
intent.putExtra("kg", kg);
intent.putExtra("inaltime", inaltime);
ctx.startActivity(intent);
}
else
{
Toast.makeText(ctx,"Autentificare esuata....",Toast.LENGTH_LONG).show();
}
}
}
Explains: I wanna inheritance the variable "cod_client" in "MyActivity" from the "Background" class. I have commented where the FIRST, SECOND AND THIRD CLASS are beginning because stack overflow doesn't recognize that they are three separated files(class). What is received in "a" variable is NULL from background class. I need to inheritance some values in "a" variable so I can use it in all my app classes. Hope now I have achieved to make myself clear. Thanks in advance for all your time spent to help me.
L.E. Added missing parts