【发布时间】:2018-01-26 14:17:52
【问题描述】:
这里有三个activities,其中一个是login activity。每当我单击login button 时,两个活动同时出现。另外两个activities 是:VendorAccount.java 和Promotion.java。代码如下:
这里我使用了intent,只要logging in的任务成功,它就会启动Promotion.class。
Login.java
public class Login extends AppCompatActivity {
private static final String TAG = "Login";
private FirebaseAuth mAuth;
private Button loginButton,signupButton;
EditText eml,pass;
String email,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
loginButton=(Button)findViewById(R.id.btn_login);
signupButton=(Button)findViewById(R.id.newuser);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
eml=findViewById(R.id.edt_email);
pass=findViewById(R.id.edt_password);
email=eml.getText().toString();
password=pass.getText().toString();
// startActivity(new Intent(Login.this,Promotion.class));
mAuth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
startActivity(new Intent(Login.this,Promotion.class));
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(Login.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
//updateUI(null);
}
// ...
}
});
}
});
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Login.this,Signup.class));
}
});
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
//updateUI(currentUser);
}
}
现在,当我单击signin 按钮时,它应该会打开Promotion.class,但同时也会打开VendorAccount.class。
Promotion.java
public class Promotion extends AppCompatActivity {
private BottomBar bottomBar;
private Button coupons,scanqr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.promotion);
Toast.makeText(getApplicationContext(),
"Promotion called",Toast.LENGTH_LONG).show();
coupons=(Button)findViewById(R.id.coupons);
scanqr=(Button)findViewById(R.id.scanqr);
bottomBar=BottomBar.attach(this,savedInstanceState);
bottomBar.setItems(R.menu.bottombars_menu);
bottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
@Override
public void onMenuTabSelected(int menuItemId) {
if (menuItemId==R.id.tab_account){
//Toast.makeText(getApplicationContext(),"Accounts",Toast.LENGTH_SHORT).show();
startActivity(new Intent(Promotion.this,VendorAccount.class));
} else if (menuItemId==R.id.tab_details){
//Toast.makeText(getApplicationContext(),"Customer Details",Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),ShowCustomer.class));
} else if (menuItemId==R.id.tab_coupons) {
//Toast.makeText(getApplicationContext(),"Coupons",Toast.LENGTH_SHORT).show();
startActivity(new Intent(Promotion.this,ShowCoupons.class));
}
}
@Override
public void onMenuTabReSelected(int menuItemId) {
}
});
// Setting colors for different tabs when there's more than three of them.
// You can set colors for tabs in three different ways as shown below.
bottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent));
bottomBar.mapColorForTab(1, 0xFF5D4037);
bottomBar.mapColorForTab(2, "#7B1FA2");
coupons.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Promotion.this,Coupons.class));
}
});
scanqr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"QR scan",Toast.LENGTH_SHORT).show();
configure_button();
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Necessary to restore the BottomBar's state, otherwise we would
// lose the current tab on orientation change.
bottomBar.onSaveInstanceState(outState);
}
private void configure_button() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA}
, 0);
}
return;
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED ){
startActivity(new Intent(Promotion.this,QRScan.class));
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == 0){
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED ){
new Thread(new Runnable() {
@Override
public void run() {
configure_button();
startActivity(new Intent(Promotion.this,QRScan.class));
}
}).start();
}else{
Toast.makeText(getApplicationContext(), "Access Denied ! Plesae Choose Camera Access Manually ", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onBackPressed() {
AlertDialog alertDialog = new AlertDialog.Builder(Promotion.this).create();
alertDialog.setTitle("System Message!!");
alertDialog.setMessage("Hey There,!!"+"\n"+"Do Tou Really Want to Leave?");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
VendorAccount.java
public class VendorAccount extends AppCompatActivity {
private ImageView displayPic;
private EditText name,addr1,addr2,phno;
private Button save;
private Button image1,image2,image3,image4;
String vendorname,vendoraddr1,vendoraddr2,vendorphno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vendor_account);
Toast.makeText(getApplicationContext(),
"VendorAccount called",Toast.LENGTH_LONG).show();
displayPic=(ImageView)findViewById(R.id.displaypicture);
image1=(Button) findViewById(R.id.image1);
image2=(Button) findViewById(R.id.image2);
image3=(Button) findViewById(R.id.image3);
image4=(Button) findViewById(R.id.image4);
save=(Button)findViewById(R.id.saveButton);
name=(EditText)findViewById(R.id.vendorname);
addr1=(EditText)findViewById(R.id.vendoradd1);
addr2=(EditText)findViewById(R.id.vendoradd2);
phno=(EditText)findViewById(R.id.vendorphno);
displayPic.setOnClickListener(choosePic);
image1.setOnClickListener(choosePic);
image2.setOnClickListener(choosePic);
image3.setOnClickListener(choosePic);
image4.setOnClickListener(choosePic);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Saved in database",Toast.LENGTH_SHORT).show();
vendorname=name.getText().toString();
vendoraddr1=addr1.getText().toString();
vendoraddr2=addr2.getText().toString();
vendorphno=phno.getText().toString();
}
});
}
public View.OnClickListener choosePic=new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1234:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
}
}
如您所见,我已将 Toast 消息放入每个类的 onCreate 方法中,以便我知道调用了哪个 class。因此,当我单击signin 按钮时,会显示Promotion.java 的Toast 消息,然后会显示VendorAccount.java 的Toast 消息和VendorAccount.java 的布局。
谁能帮我解决这个问题?
manifest 和 activiy_login 文件如下:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vendorapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Coupons"
android:theme="@style/AppThemeNoBar" />
<activity
android:name=".Promotion"
android:label="Promotion"
android:theme="@style/AppThemeNoBar" />
<activity
android:name=".Login"
android:label="Login"
android:theme="@style/AppThemeNoBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Signup"
android:label="SignUp"
android:theme="@style/AppThemeNoBar" />
<activity
android:name=".CouponsNew"
android:theme="@style/AppThemeNoBar" />
<activity
android:name=".QRScan"
android:theme="@style/AppThemeNoBar" />
<activity android:name=".ShowCoupons"></activity>
<activity android:name=".qrinfo"></activity>
<activity
android:name=".ShowCustomer"
android:theme="@style/AppThemeNoBar" />
<activity
android:name=".VendorAccount"
android:theme="@style/AppThemeNoBar">
</activity>
</application>
activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context="com.example.vendorapp.Login">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20sp"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edt_email"
android:layout_width="match_parent"
android:layout_height="50sp"
android:drawablePadding="10sp"
android:drawableStart="@drawable/ic_account_circle_white_24dp"
android:drawableLeft="@drawable/ic_account_circle_white_24dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:paddingEnd="10sp"
android:paddingStart="10sp"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:backgroundTint="@color/white" />
<EditText
android:id="@+id/edt_password"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_marginTop="15sp"
android:drawablePadding="10sp"
android:drawableStart="@drawable/ic_lock_white_24dp"
android:drawableLeft="@drawable/ic_lock_white_24dp"
android:hint="Password"
android:inputType="textPassword"
android:paddingEnd="10sp"
android:paddingStart="10sp"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:backgroundTint="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/buttonPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="15sp"
android:background="@color/white">
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:text="Login"
android:textColor="#000000"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="@+id/newuser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:gravity="center"
android:background="?attr/selectableItemBackground"
android:text="New User? Register Here."
android:textColor="@android:color/white"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30sp"
android:layout_height="30sp"
android:layout_gravity="center"
android:layout_marginBottom="20sp"
android:visibility="gone" />
【问题讨论】:
-
您能否也发布您的 Manifest 和 activity_login.xml 文件?
-
@LeoNeo,我已经添加了
-
您正在为底栏使用外部库(我猜这是github.com/roughike/BottomBar)?无论如何,如果是这样,则不推荐使用该库,这可能会导致兼容性问题(例如答案中提到的问题)。我的建议是改用这个github.com/aurelhubert/ahbottomnavigation
标签: java android android-layout android-intent