我有以下问题。 我已经实现了一个必须连接到在线数据库的应用程序,我想将一个客户端代码继承到一个变量,所以我可以将它从用户界面的一个活动用于我需要它的所有其他活动,这样我就可以进行SQL查询。 我的问题是新活动继承了一个空值。
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();
}
}
}
说明: 我想从“背景”类继承“MyActivity”中的变量“cod_client”。 我评论了FIRST,SECOND和THIRD CLASS的开始,因为堆栈溢出无法识别它们是三个独立的文件(类)。 “a”变量中收到的内容是背景类中的NULL。 我需要在“a”变量中继承一些值,这样我就可以在我的所有app类中使用它。希望现在我已经达到了让自己清醒的目标。 提前感谢您花费的所有时间来帮助我。
L.E。添加缺失的部分