我有一个工具栏,我在XML中添加了一个include语句。但是在MainActivity onCreate中,我收到此错误,请帮助我。 我的主要活动课程:
package com.sample.passwordreset;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.support.v7.widget.Toolbar;
import com.parse.ParseUser;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setContentView(toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(getResources().getColor(R.color.colorAccent));
}
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser == null) { //User not logged in
Log.d("MainAcitvity", "Sending to LoginActivity!");
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</RelativeLayout>
这是我的toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
android:paddingRight="0dp"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:titleTextAppearance="@style/AppTheme.Toolbar.Title"
android:theme="@style/AppTheme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
日志cat错误
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3672)
at android.view.ViewGroup.addView(ViewGroup.java:3543)
at android.view.ViewGroup.addView(ViewGroup.java:3488)
at android.view.ViewGroup.addView(ViewGroup.java:3464)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:247)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:114)
at com.sample.passwordreset.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5177)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
02-05 13:02:56.358 11190-11190/com.sample.passwordreset I/Process: Sending signal. PID: 11190 SIG: 9
LoginActivity
public class LoginActivity extends NoAuthActivity {
private EditText mUsernameField;
private EditText mPasswordField;
private TextView mErrorField;
private ProgressBar spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
mUsernameField = (EditText) findViewById(R.id.login_email);
mPasswordField = (EditText) findViewById(R.id.login_password);
mErrorField = (TextView) findViewById(R.id.error_messages);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
spinner.setVisibility(View.GONE);
Parse.initialize(this);
Parse.setLogLevel(Parse.LOG_LEVEL_DEBUG);
ParseQuery<ParseObject> qtw = ParseQuery.getQuery("tw");
qtw.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
ParseTwitterUtils.initialize("Rakesh", "Maski");
}else
{
Log.d("tx:Error",e.getMessage());
}
}
/* public void done(ParseObject obj, ParseObject e) {
if (e == null) {
String tw_consumer_key = obj.get("keyid").toString();
String tw_consumer_secret = obj.get("secretid").toString();
//ParseTwitterUtils.initialize(tw_consumer_key, tw_consumer_secret);
} else {
//Log.d("Tw: Error", e.getMessage());
}
}*/
});
}
public void signInNormal(final View v) {
v.setEnabled(false);
spinner.setVisibility(View.VISIBLE);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(mUsernameField.getWindowToken(), 0);
if (mUsernameField.getText().length()==0 || mPasswordField.getText().length()==0){
v.setEnabled(true);
spinner.setVisibility(View.GONE);
mErrorField.setText("All the fields are required. Please fill them.");
return;
}
ParseUser.logInInBackground(mUsernameField.getText().toString(),
mPasswordField.getText().toString(), new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
if (user != null) {
boolean verify = user.getBoolean("emailVerified");
if (!verify) {
spinner.setVisibility(View.GONE);
mErrorField.setText("Are you sure you're verified? Verify by opening link we have sent to your registered email & then login again.");
} else {
spinner.setVisibility(View.GONE);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
} else {
//Signup failed. Look at the ParseException to see
// what happened.
switch (e.getCode()) {
case ParseException.USERNAME_TAKEN:
mErrorField
.setText("Sorry, this username has already been taken.");
break;
case ParseException.USERNAME_MISSING:
mErrorField
.setText("Sorry, you must supply a username to register.");
break;
case ParseException.PASSWORD_MISSING:
mErrorField
.setText("Sorry, you must supply a password to register.");
break;
case ParseException.OBJECT_NOT_FOUND:
mErrorField
.setText("Sorry, those credentials were invalid.");
break;
case ParseException.CONNECTION_FAILED:
mErrorField
.setText("Internet connection was not found. Please see your connection settings.");
break;
default:
mErrorField.setText(e.getLocalizedMessage());
break;
}
v.setEnabled(true);
spinner.setVisibility(View.GONE);
}
}
});
}
public void showRegistration(View v) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
finish();
}
public void forgpassActivity(View v) {
Intent intent = new Intent(this, ForgotPassActivity.class);
startActivity(intent);
finish();
}
public void signInTwitter(final View v) {
v.setEnabled(false);
spinner.setVisibility(View.VISIBLE);
ParseTwitterUtils.logIn(this, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("RYC", "Uh oh. The user cancelled the Twitter login.");
spinner.setVisibility(View.GONE);
v.setEnabled(true);
} else if (user.isNew()) {
Log.d("RYC", "User signed up and logged in through Twitter!");
String userscreenName = ParseTwitterUtils.getTwitter().getScreenName();
user.put("screenName", userscreenName);
try {
user.save();
} catch (ParseException e) {
e.printStackTrace();
}
spinner.setVisibility(View.GONE);
LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
Log.d("RYC", "User logged in through Twitter!");
String userscreenName = ParseTwitterUtils.getTwitter().getScreenName();
user.put("screenName", userscreenName);
try {
user.save();
} catch (ParseException e) {
e.printStackTrace();
}
spinner.setVisibility(View.GONE);
LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
}
});
}
}
答案 0 :(得分:1)
引起:java.lang.IllegalStateException:指定的子级 已经有了父母。您必须在孩子的父母上调用removeView() 第一。 在android.view.ViewGroup.addViewInner(ViewGroup.java:3672) 在android.view.ViewGroup.addView(ViewGroup.java:3543)
为什么要拨打2次 setContentView()
错误
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setContentView(toolbar);
执行
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
一旦你有了ToolBar的实例,我们就会调用 setSupportActionBar(toolbar)并将我们的ToolBar对象传递给它。
答案 1 :(得分:0)
使用setSupportActionBar(toolbar);
而不是setContentView
答案 2 :(得分:0)
从我看到你有两次setContentView()
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
/*
The point where you get error, remove the below method,
and your code should work fine
*/
setContentView(toolbar);
}
}
您没有两次setContentView()
答案 3 :(得分:0)
将setContentView(toolbar);
更改为setSupportActionBar(toolbar);
解决你的问题