当我在app gradle中添加依赖项以获取回收站视图和卡片视图时,应用程序开始提供以下错误:
致命的例外:主要 处理:com.example.pawadube.helloworld,PID:3549 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.pawadube.helloworld / com.example.pawadube.helloworld.ApplicationActivity}:java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏。不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102)
我的gradle文件如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.pawadube.helloworld"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:+'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'com.android.support:design:26.0.0'
compile 'com.squareup.okhttp3:okhttp:3.9.1'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'com.android.support:cardview-v7:27.0.2'
}
MainActivity类
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class LoginActivity extends AppCompatActivity {
EditText etServerURL, etUsername, etPassword;
Button buttonConnect;
ProgressDialog progressDoalog;
String serverURL, username, password;
String loginURL = "/rest/allData/index";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findId();
buttonConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginButtonClicked(view);
}
});
}
void loginButtonClicked(View view) {
progressDoalog = new ProgressDialog(LoginActivity.this);
progressDoalog.setTitle("Connecting...");
progressDoalog.setCancelable(false);
progressDoalog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
boolean flag = validateField();
if (!isNetworkAvailable()) {
Snackbar snackbar = Snackbar.make(view, "Please check internet connectvity.", Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
snackbar.show();
} else if (flag == true) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String url = etServerURL.getText().toString() + loginURL;
new LoginServerTest(view).execute(username, password, url);
} else {
Snackbar snackbar = Snackbar.make(view, "Please fill all the details.", Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.RED);
snackbar.show();
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
void findId() {
etServerURL = (EditText) findViewById(R.id.serverURL);
etUsername = (EditText) findViewById(R.id.username);
etPassword = (EditText) findViewById(R.id.password);
buttonConnect = (Button) findViewById(R.id.connect);
}
boolean validateField() {
boolean flag = true;
serverURL = etServerURL.getText().toString().trim();
username = etUsername.getText().toString().trim();
password = etPassword.getText().toString().trim();
if (serverURL.equals("")) {
etServerURL.setError("Server address is mandatory.");
etServerURL.requestFocus();
flag = false;
} else if (username.contentEquals("")) {
etUsername.setError("Username is mandatory.");
etUsername.requestFocus();
flag = false;
} else if (password.contentEquals("")) {
etPassword.setError("Password is mandatory.");
etPassword.requestFocus();
flag = false;
} else if (!URLUtil.isValidUrl(serverURL)) {
etServerURL.setError("Entered server address is invalid");
etServerURL.requestFocus();
flag = false;
}
return flag;
}
void clearFields() {
etServerURL.setText("");
etUsername.setText("");
etPassword.setText("");
}
@Override
protected void onDestroy() {
super.onDestroy();
if (progressDoalog != null) {
progressDoalog.dismiss();
progressDoalog = null;
}
}
class LoginServerTest extends AsyncTask<String, Integer, String[]> {
private View rootView;
public LoginServerTest(View rootView) {
this.rootView = rootView;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDoalog.show();
}
@Override
protected String[] doInBackground(String... strings) {
String username = strings[0];
String password = strings[1];
String url = strings[2];
String credential = Credentials.basic(username, password);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("authorization", credential)
.addHeader("content-type", "application/json")
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String body = response.body().string();
String code = response.code() + "";
String[] output = {code, body};
return output;
} else {
String body = "Error: 404";
String code = response.code() + "";
String[] output = {code, body};
return output;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String[] strings) {
super.onPostExecute(strings);
progressDoalog.dismiss();
if (strings != null) {
if (strings[0].contentEquals("200")) {
Intent intent = new Intent(LoginActivity.this, TabActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "Logged in successfully", Toast.LENGTH_LONG).show();
clearFields();
} else if (strings[0].contentEquals("401")) {
Snackbar snackbar = Snackbar.make(rootView, "Username/password is invalid, please try again.", Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.RED);
snackbar.show();
} else if (strings[0].contentEquals("404")) {
Snackbar snackbar = Snackbar.make(rootView, strings[1] + ", please check the server URL.", Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.RED);
snackbar.show();
} else {
Snackbar snackbar = Snackbar.make(rootView, strings[0] + " please try again.", Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.RED);
snackbar.show();
}
} else {
Snackbar snackbar = Snackbar.make(rootView, "Oops something went wrong, please check the server address.", Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.RED);
snackbar.show();
}
}
}
}
Styles.xml文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="EditText">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TextView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/textColor</item>
</style>
<style name="Tab">
<item name="android:textSize">12dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#ffffff</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
答案 0 :(得分:0)
尝试使用旧版本,它对我有用:
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.android.support:cardview-v7:26.0.0-alpha1'
答案 1 :(得分:0)
尝试共享styles.xml文件。
如果有这个:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
而不仅仅是改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
并保持所有其他行与往常一样。