我是Android编程新手,这是我第一个使用Gradle的项目。在过去,我使用Maven for Java。所以我使用Android工作室制作了一个应用程序,如果我点击运行并选择我的手机,它将构建并安装apk到我的手机,然后自行打开并正常工作。
所以我想与我的一位朋友分享我的进展,所以我点击制作项目,然后生成 app-debug.apk 在 /app/build/outputs/apk/app-debug.apk 中,但如果我尝试在启动后使用文本安装我的应用程序强制关闭本身:" 不幸的是,该应用程序停止了工作"。
以下是我的 build.gradle (模块:应用):
和我的 build.gradle (项目:Autosiskola):
错误代码:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidports.autosiskola/com.androidports.autosiskola.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.androidports.autosiskola.MainActivity" on path: DexPathList[[zip file "/data/app/com.androidports.autosiskola-1/base.apk"],nativeLibraryDirectories=[/data/app/com.androidports.autosiskola-1/lib/arm64, /vendor/lib64, /system/lib64]]
我的主要活动:
package com.androidports.autosiskola;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
EditText username, password;
Button login;
ProgressBar progressBar;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
username = (EditText) findViewById(R.id.txtUsername);
password = (EditText) findViewById(R.id.txtPassword);
login = (Button) findViewById(R.id.btnLogin);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
conn = DBConnect.Connector();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (username.toString().trim().isEmpty() && password.toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Nincs felhasználónév / jelszó megadva!", Toast.LENGTH_SHORT).show();
} else {
switch (v.getId()) {
case R.id.btnLogin:
new doMySQL().execute();
break;
}
}
}
});
}
public class doMySQL extends AsyncTask<Void, Void, String> {
final String uname = username.getText().toString();
final String passwd = password.getText().toString();
String getUsername = null;
String getPassword = null;
String getPermission = null;
String resValue = "undone";
int exists = 0;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
protected String doInBackground(Void... params) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
conn = DBConnect.Connector();
try {
pst = conn.prepareStatement("SELECT username FROM userList WHERE username=?");
pst.setString(1, String.valueOf(uname));
rs = pst.executeQuery();
if (rs.next()) {
getUsername = rs.getString(1);
}
pst = conn.prepareStatement("SELECT username FROM userList WHERE username=?");
pst.setString(1, String.valueOf(uname));
rs = pst.executeQuery();
if (rs.wasNull()) {
Toast.makeText(MainActivity.this, "Hibás felhasználónév / jelszó!", Toast.LENGTH_SHORT).show();
resValue = "undone";
}
pst = conn.prepareStatement("SELECT password FROM userList WHERE username=?");
pst.setString(1, String.valueOf(uname));
rs = pst.executeQuery();
if (rs.next()) {
getPassword = rs.getString(1);
}
pst = conn.prepareStatement("SELECT password FROM userList WHERE username=?");
pst.setString(1, String.valueOf(uname));
rs = pst.executeQuery();
if (rs.wasNull()) {
Toast.makeText(MainActivity.this, "Hibás felhasználónév / jelszó!", Toast.LENGTH_SHORT).show();
resValue = "undone";
}
pst = conn.prepareStatement("SELECT username FROM userList WHERE username=? AND password=?");
pst.setString(1, String.valueOf(uname));
pst.setString(2, String.valueOf(passwd));
rs = pst.executeQuery();
if (rs.next()) {
exists = 1;
}
pst = conn.prepareStatement("SELECT username FROM userList WHERE username=? AND password=?");
pst.setString(1, String.valueOf(uname));
pst.setString(2, String.valueOf(passwd));
if (rs.wasNull()) {
Toast.makeText(MainActivity.this, "Hibás felhasználónév / jelszó!", Toast.LENGTH_SHORT).show();
exists = 0;
resValue = "undone";
}
if (exists == 1) {
pst = conn.prepareStatement("SELECT permission FROM userList WHERE username=?");
pst.setString(1, String.valueOf(uname));
rs = pst.executeQuery();
while (rs.next()) {
getPermission = rs.getString(1);
resValue = "Done";
}
}
} catch (SQLException e) {
Toast.makeText(MainActivity.this, "Hibás felhasználónév / jelszó!", Toast.LENGTH_SHORT).show();
}
return resValue;
}
protected void onPostExecute(String result) {
if ("Done".equals(result)) {
progressBar.setVisibility(View.GONE);
if ("teacher".equals(getPermission)) {
Intent openTeacher = new Intent(MainActivity.this, TeacherLogin.class);
progressBar.setVisibility(View.GONE);
startActivity(openTeacher);
} else if ("student".equals(getPermission)) {
Intent openStudent = new Intent(MainActivity.this, StudentLogin.class);
progressBar.setVisibility(View.GONE);
startActivity(openStudent);
}
} else if ("undone".equals(result)) {
Toast.makeText(MainActivity.this, "Hibás felhasználónév / jelszó!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
}
}
当我使用USB上的“运行”命令安装应用程序时,该应用程序正常工作,但如果我想安装app-debug.apk,则无法正常工作。
我的lib文件夹中有一个mysql-connector-java依赖项,这就是全部。
答案 0 :(得分:0)
有趣的事实:当我使用Windows资源管理器将app-debug.apk复制到我的SDCard时,应用程序会在启动后自行停止。但是如果我打开一个新命令行并创建一个:
adb install app-debug.apk
在 outputs / apk / 文件夹中,它可以正常工作。我认为这是解决方案。
答案 1 :(得分:0)
您无法复制文件outputs/apk/app-debug.apk
以在Android设备中运行,但也不适用于我。当我尝试安装崩溃的app-debug.apk
时。我正在尝试首先签署应用程序并选择调试。
要做到这一点:
构建 - &gt;生成签名APK
如果您没有密钥库路径,请填空,请创建新路径。 https://developer.android.com/studio/publish/app-signing.html
选择构建类型为debug并选择V1和V2
等到完成。