安卓 java。lang.IllegalArgumentException:在给定视图中找不到合适的父级。请提供有效的视图
我的应用程序在试图显示服务器消息时崩溃,我认为问题可能出在我的getView()
。下面是崩溃发生时的registerActivity
和我的activity_register.xml
import 安卓.app.Activity;
import 安卓.app.ProgressDialog;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.support.design.widget.Snackbar;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ProgressBar;
import 安卓.widget.Toast;
import com.fastchat.helper.SQLiteHandler;
import com.fastchat.helper.SessionManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RegisterActivity extends Activity {
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnRegister;
private Button btnLinkToLogin;
private EditText inputFullName;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
private ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
inputFullName = (EditText) findViewById(R.id.name);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(true);
/*
// Session manager
session = new SessionManager(getApplicationContext());
// SQLite database handler
db = new SQLiteHandler(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(RegisterActivity.this,
SearchableActivity.class);
startActivity(intent);
finish();
}
*/
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
registerProcess(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Please enter your details!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
finish();
}
});
}
private void registerProcess(String name,String email,String password){
String tag_string_req = "req_register";
//pDialog = new AlertDialog.Builder(getActivity());
pDialog.setMessage("please wait");
pDialog.show();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
User user = new User();
user.setName(name);
user.setEmail(email);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.REGISTER_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
private View view;
public View getView() {
return view;
}
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
if(resp !=null)//tried to check if resp is null but its not
//crash occurs here
Snackbar.make(getView(),resp.getMessage(), Snackbar.LENGTH_LONG).show();
pDialog.dismiss();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
// progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG, "failed");
Toast.makeText(getApplicationContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
});
}
activity_register.xml
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:background="@color/bg_register"
安卓:gravity="center"
安卓:orientation="vertical"
安卓:padding="10dp"
安卓:id="@+id/chat">
<LinearLayout
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center"
安卓:orientation="vertical"
安卓:paddingLeft="20dp"
安卓:paddingRight="20dp" >
<ImageView
安卓:layout_width="389dp"
安卓:layout_height="72dp"
安卓:layout_gravity="center_horizontal"
安卓:layout_marginBottom="24dp"
安卓:src="@drawable/logo"
tools:ignore="ContentDescription" />
<EditText
安卓:id="@+id/name"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="10dp"
安卓:background="@color/input_register_bg"
安卓:hint="@string/hint_name"
安卓:padding="10dp"
安卓:singleLine="true"
安卓:inputType="textCapWords"
安卓:textColor="@color/input_register"
安卓:textColorHint="@color/input_register_hint" />
<EditText
安卓:id="@+id/email"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="10dp"
安卓:background="@color/input_register_bg"
安卓:hint="@string/hint_email"
安卓:inputType="textEmailAddress"
安卓:padding="10dp"
安卓:singleLine="true"
安卓:textColor="@color/input_register"
安卓:textColorHint="@color/input_register_hint" />
<EditText
安卓:id="@+id/password"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="10dp"
安卓:background="@color/input_register_bg"
安卓:hint="@string/hint_password"
安卓:inputType="textPassword"
安卓:padding="10dp"
安卓:singleLine="true"
安卓:textColor="@color/input_register"
安卓:textColorHint="@color/input_register_hint" />
<!-- Login Button -->
<Button
安卓:id="@+id/btnRegister"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="20dip"
安卓:background="#ea4c88"
安卓:text="register"
安卓:textColor="@color/white" />
<!-- Link to Login Screen -->
<Button
安卓:id="@+id/btnLinkToLoginScreen"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="40dip"
安卓:background="@null"
安卓:text="already a member? login"
安卓:textAllCaps="false"
安卓:textColor="@color/white"
安卓:textSize="15dp" />
</LinearLayout>
下面是logcat消息
java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
at 安卓.support.design.widget.Snackbar.make(Snackbar.java:137)
at com.chat.RegisterActivity$3.onResponse(RegisterActivity.java:143)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
# 1 楼答案
我也有这个问题。。。当然有点不同
# 2 楼答案
我建议你试试
findViewById(android.R.id.content)
这就是我的诀窍:
android.R.id.content
将为您提供视图的根元素(有关该元素的更多信息,请查看Android: What is android.R.id.content used for?)# 3 楼答案
我找到了一个解决方案,可以帮助其他寻求共同解决方案的人
在加载整个XML文件之前,我试图用Snackbar显示消息时出现了这个错误
在片段中,我尝试在
onCreateView();
中调用API,并显示Internet connection is off
的Snackbar
消息,如下所示:我只是把这个方法放在
onViewCreated()
它现在对我有用
奖金:
多谢各位
# 4 楼答案
使用协调布局将布局包装起来
# 5 楼答案
使用KOTLIN
或者如果您在活动中简单地删除
activity?.
# 6 楼答案
用这个替换您的视图
getActivity().getCurrentFocus()
但不是在默认片段中