- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果 Android 版本是 Kitkat 或更高版本,我尝试使用 Java 添加 marginTop 20。我收到错误指定的子项已经有父项。您必须首先对子级的父级调用removeView()。我已经尝试过这些但没有效果。
MainActivity.java
package com.appex.tryproject;
import android.app.ActionBar;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.gc.materialdesign.views.LayoutRipple;
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
import com.readystatesoftware.systembartint.SystemBarTintManager;
public class MainActivity extends ActionBarActivity implements View.OnClickListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private CallbackManager callbackManager;
LoginButton loginButton;
private static final int RC_SIGN_IN = 0;
private GoogleApiClient mGoogleApiClient;
private boolean mSignInClicked;
ProgressBarCircularIndeterminate p;
/**
* A flag indicating that a PendingIntent is in progress and prevents us
* from starting further intents.
*/
private boolean mIntentInProgress;
private ConnectionResult mConnectionResult;
private SignInButton btnSignIn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.toolbar_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_r);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
LinearLayout Main_layout=(LinearLayout)findViewById(R.id.main_layout);
LinearLayout linearLayout=new LinearLayout(this);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 25, 0, 0);
linearLayout.removeAllViews();
linearLayout.addView(Main_layout, layoutParams);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintColor(getResources().getColor(R.color.primary_dark));
}
toolbar.setTitleTextColor(getResources().getColor(R.color.primary_text));
setSupportActionBar(toolbar);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(), "Login successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), EventActivity.class);
startActivity(intent);
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Login canceled", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), EventActivity.class);
startActivity(intent);
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "Login error", Toast.LENGTH_SHORT).show();
}
});
TextView orText=(TextView)findViewById(R.id.optionView);
orText.setGravity(Gravity.CENTER);
LayoutRipple RegisterButton=(LayoutRipple)findViewById(R.id.register);
RegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),RegisterActivity.class);
startActivity(intent);
}
});
com.rey.material.widget.Button LoginEmail=(com.rey.material.widget.Button)findViewById(R.id.emailLogin);
LoginEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),EmailActivity.class);
startActivity(intent);
}
});
btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
// Button click listeners
btnSignIn.setOnClickListener(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
p = (ProgressBarCircularIndeterminate)findViewById(R.id.progressBarCircularIndeterminate);
p.setBackgroundColor(getResources().getColor(R.color.accent));
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
p.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
p.setVisibility(View.VISIBLE);
}
}
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onConnected(Bundle bundle) {
p.setVisibility(View.GONE);
mSignInClicked = false;
Intent intent = new Intent(MainActivity.this,EventActivity.class);
intent.putExtra("name",Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName());
intent.putExtra("mode","google");
startActivity(intent);
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_sign_in:
// Signin button clicked
signInWithGplus();
break;
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
@Override
protected void onResume() {
super.onResume();
if(mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
// Logs 'install' and 'app activate' App Events.
AppEventsLogger.activateApp(this);
}
@Override
protected void onPause() {
super.onPause();
if(mGoogleApiClient.isConnected()){
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);}
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}
}
main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:id="@+id/container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout">
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center"
/>
<com.google.android.gms.common.SignInButton
android:id="@+id/btn_sign_in"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:layout_gravity="center"
/>
<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
android:id="@+id/progressBarCircularIndeterminate"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#1E88E5"
android:layout_gravity="center"
android:visibility="gone"
/>
<com.rey.material.widget.Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_email"
android:text="@string/emailButton"
style="@style/buttong"
android:textColor="@color/primary_text"
android:id="@+id/emailLogin"
android:textAllCaps="false"
android:textSize="18.5sp"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:text="@string/option"
android:textColor="@color/secondary_text"
android:textSize="18.5sp"
android:gravity="center"
android:layout_below="@id/linearLayout"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:id="@+id/optionView" />
<com.gc.materialdesign.views.LayoutRipple
android:id="@+id/register"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.gc.materialdesign.views.ButtonFlat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18.5sp"
android:text="@string/reg"
android:layout_margin="10dp"
android:gravity="center"
android:background="@color/primary_dark"
/>
</com.gc.materialdesign.views.LayoutRipple>
</LinearLayout>
</RelativeLayout>
toolbar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/primary" />
<include
layout="@layout/main"/>
</LinearLayout>
日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appex.tryproject/com.appex.tryproject.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at dalvik.system.NativeStart.main(Native Method)
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:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.view.ViewGroup.addView(ViewGroup.java:3391)
at com.appex.tryproject.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at dalvik.system.NativeStart.main(Native Method)
我收到错误指定的子项已经有父项。您必须首先在子级的父级上调用removeView(),位于 com.appex.tryproject.MainActivity.onCreate(MainActivity.java:63)这是linearLayout.addView(Main_layout, layoutParams);
最佳答案
您可以更改此设置:
linearLayout.removeAllViews();
对此:
((ViewGroup)Main_layout.getParent()).removeView(Main_Layout);
线性布局还没有任何 View (您刚刚创建了它),因此线性布局.removeAllViews() 调用是多余的。您需要的不是从 Main_Layout 的新父级中删除所有 View ,而是从其先前的父级中删除 Main_Layout。
为了使代码更具可读性,我可能会做的是将 Main_Layout 包装在另一个布局中,这样您就可以执行如下操作:
LinearLayout Main_layout=(LinearLayout)findViewById(R.id.main_layout);
RelativeLayout wrapper = (RelativeLayout)findViewById(R.id.wrapper);
wrapper.removeView(Main_layout);
LinearLayout linearLayout=new LinearLayout(this);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 25, 0, 0);
linearLayout.addView(Main_layout, layoutParams);
wrapper.addView(linearLayout);
//more code
然后是您的toolbar_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wrapper">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/primary" />
<include layout="@layout/main"/>
</LinearLayout>
</RelativeLayout>
关于java - 指定的子项已经有父项。您必须首先对 child 的 parent 调用removeView()”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955838/
一段时间以来,我一直在做这个反复出现的噩梦(阅读 - 我的应用程序中的错误)。出于某种原因,某个计时器在我停止后继续发送“Elapsed”事件,即使 在事件本身 计时器“承认”已被禁用!检查一下: /
为了找到 2 个 git 分支的共同祖先,需要做的是: git merge-base branch another_branch 好的。但是……如果两个分支都已经 merge 了怎么办?当我在这种情况
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我想要一个相机 View ,可以将图像捕获到本地文件或让用户从本地照片库中选择图像。我想也许有人为此编写了很好的库/代码。也许我可以利用它。已经有好的了吗?谢谢。我只是避免重新发明轮子:) 最佳答案
我从 master 分支创建了一个功能分支。之后有来自功能分支的提交 [F1]。 [F1] -- Feature Branch / [M1]-[M2
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
介绍和搜索 所以我认为我犯了一个严重的错误,我很担心。我已经分析了独立负责人的论坛,我已经接近找到答案,但场景太具体,不适用于我所在的位置。如果您找到可以回答我的问题的特定主题,请链接我。 例如:Ho
我有一个类似于下图的提交图。标记为 * 的提交表示大量提交。 A* | B--------- | | C* D* master 和 cor
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
我们最近接手了一个 .NET 项目,在查看 db 后,我们在某些列中有以下内容: 1)某些列具有诸如" & etc etc 2) 有些有 标签和其他非 html 编码的标签 这些数据
你好,当我导航到应用程序中的另一个页面时出现此错误 我不知道为什么这个错误出现 #0 _AsyncCompleter.complete (dart:async/future_impl.da
我使用以下 C 算法计算数据的 CRC32: #define CRC32_POLYNOM_REVERSED 0xEDB88320 uint32 calcCrc32(uint8* buffer, u
我试图在我的一个测试中断言模型中的字段没有改变。我知道从哲学上这是不正确的,但由于我控制了我需要知道的所有变量,所以我只想检查我的数据库条目是否没有改变。 我愿意接受一个解决方案,该解决方案可以将其转
我是 GitHub 的新手。并通过 Eclipse 使用它我们是两个人在开发一个应用程序。当我在 Git shell 中检查 git status 时,我得到以下状态。 On branch maste
简单代码: std::ifstream file("file.txt"); std::string line; while(getline(file,line)) ; //exhaust file
是的,我又找不到这个 Gradle DSL 方法:'compile()' 问题。 我检查了我有: buildscript { repositories { jcenter()
HTML: articles CSS: #main_menu { float: left; padding-top: 10px; vertical-align: m
我是一名优秀的程序员,十分优秀!