切换到firebase-应用程序的活动关闭 [英] Switching Activity to firebase- app shuts down

查看:270
本文介绍了切换到firebase-应用程序的活动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击MainActivity中的一个链接时,我有一个问题切换活动,应用程序将关闭,我不知道如果我有项目结构的问题或正确声明类和扩展。 / p>

基本上我所做的就是尝试将网站上提供的Firebase示例项目合并到我的应用中的一个活动中,以便让成员登录,我把最初的代码分成了这里并在我的第二个活动窗口中实现它,链接中的代码是MainActivity,我希望它作为LoginFragment(只有一个名称)没有使用片段。



因此,在最小化代码(删除谷歌,Facebook等)选项后,我结束了这个:

  package net.we4x4.we4x4; 

导入android.app.AlertDialog;
导入android.app.Application;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AppCompatActivity;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.View;
导入android.widget.Button;
导入android.widget.TextView;

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;

import java.util.HashMap;
import java.util.Map;

public class LoginFragment extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

/ * *************************************
*一般*
*************************************** /
/ * TextView用于显示登录用户的信息* /
private TextView mLoggedInStatusTextView;

/ *在Firebase身份验证完成之前显示的对话框。 * /
私人ProgressDialog mAuthProgressDialog;

/ *引用Firebase * /
私有Firebase mFirebaseRef;

/ *来自认证用户的数据* /
private AuthData mAuthData;

/ * Firebase会话更改的监听器* /
私有Firebase.AuthStateListener mAuthStateListener;

/ * *************************************
*密码*
*************************************** /
私人按钮mPasswordLoginButton;

/ * *************************************
* ANONYMOUSLY *
*************************************** /
私人按钮mAnonymousLoginButton;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/ *加载视图并显示它* /
setContentView(R.layout.activity_main);

/ * *************************************
*密码*
*************************************** /
mPasswordLoginButton =(Button)findViewById(R.id.login_with_password);
mPasswordLoginButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
loginWithPassword();
}
});

/ * *************************************
* ANONYMOUSLY *
*************************************** /
/ *加载并设置匿名登录按钮* /
mAnonymousLoginButton =(Button)findViewById(R.id.login_anonymously);
mAnonymousLoginButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
loginAnonymously();
}
});

/ * *************************************
*一般*
*************************************** /
mLoggedInStatusTextView =(TextView)findViewById(R.id.login_status);
$ b $ *创建用于Firebase * /
的所有身份验证的Firebase引用mFirebaseRef = new Firebase(getResources()。getString(R.string.firebase_url));
$ b $ *设置稍后使用Firebase进行身份验证时显示的进度对话框* /
mAuthProgressDialog = new ProgressDialog(this);
mAuthProgressDialog.setTitle(Loading);
mAuthProgressDialog.setMessage(使用Firebase进行身份验证...);
mAuthProgressDialog.setCancelable(false);
mAuthProgressDialog.show();

mAuthStateListener = new Firebase.AuthStateListener(){
@Override
public void onAuthStateChanged(AuthData authData){
mAuthProgressDialog.hide();
setAuthenticatedUser(authData);
}
};
/ *检查用户是否已经使用Firebase进行身份验证。如果是这种情况,我们可以设置认证的
*用户并隐藏任何登录按钮* /
mFirebaseRef.addAuthStateListener(mAuthStateListener);


@Override
protected void onDestroy(){
super.onDestroy();


//如果更改配置,请停止跟踪Firebase会话。
mFirebaseRef.removeAuthStateListener(mAuthStateListener);
}

/ **
*此方法在任何startActivityForResult完成时触发。 requestCode映射到
*传递给startActivityForResult的值。
* /
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
Map< String,String> options = new HashMap< String,String>();


$覆盖
public boolean onCreateOptionsMenu(菜单菜单){
/ *如果用户当前已被认证,显示一个注销菜单* /
if(this.mAuthData!= null){
getMenuInflater()。inflate(R.menu.main,menu);
返回true;
} else {
return false;


$ b $ @覆盖
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_logout){
logout();
返回true;
}
return super.onOptionsItemSelected(item);
}

/ **
*如果有必要,请从Firebase和提供商处取消验证。
* /
private void logout(){
if(this.mAuthData!= null){
/ * Firebase * / $ b $注销mFirebaseRef.unauth() ;
}
/ *更新认证用户并显示登录按钮* /
setAuthenticatedUser(null);


$ b / **
*这个方法将尝试认证一个用户firebase给定一个oauth_token(和其他
*必要的参数取决于提供者)
* /
private void authWithFirebase(final String提供者,Map< String,String>选项){
if(options.containsKey(error)){
showErrorDialog(options.get( 错误));
} else {
mAuthProgressDialog.show();
if(provider.equals(twitter)){
//如果提供者是twitter,我们会传入其他选项,所以使用选项端点
mFirebaseRef.authWithOAuthToken(provider,选项,新的AuthResultHandler(provider));
} else {
//如果提供者不是twitter,我们只需要传入oauth_token
mFirebaseRef.authWithOAuthToken(provider,options.get(oauth_token),new AuthResultHandler提供商));



$ b $ ** b $ b *一旦用户登录,从Firebase提供的mAuthData中使用它。
* /
private void setAuthenticatedUser(AuthData authData){
if(authData!= null){
/ *隐藏所有登录按钮* /
mPasswordLoginButton.setVisibility (View.GONE);
mAnonymousLoginButton.setVisibility(View.GONE);
mLoggedInStatusTextView.setVisibility(View.VISIBLE);
/ *显示提供商特定状态文本* /
字符串名称= null;
if(authData.getProvider()。equals(facebook)
|| authData.getProvider()。equals(google)
|| authData.getProvider()。equals twitter)){
name =(String)authData.getProviderData()。get(displayName);
} else if(authData.getProvider()。equals(anonymous)
|| authData.getProvider()。equals(password)){
name = authData.getUid( );
} else {
Log.e(TAG,Invalid provider:+ authData.getProvider());
}
if(name!= null){
mLoggedInStatusTextView.setText(Logged in+ name +(+ authData.getProvider()+));

} else {
/ *没有经过身份验证的用户显示所有登录按钮* /
mPasswordLoginButton.setVisibility(View.VISIBLE);
mAnonymousLoginButton.setVisibility(View.VISIBLE);
mLoggedInStatusTextView.setVisibility(View.GONE);
}
this.mAuthData = authData;
/ *无效选项菜单隐藏/显示注销按钮* /
supportInvalidateOptionsMenu();



$ b / **
*向用户显示错误
* /
private void showErrorDialog(String message) {
AlertDialog.Builder(this)
.setTitle(Error)
.setMessage(message)
.setPositiveButton(android.R.string.ok,null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();

$ b $ **
*认证结果的工具类
* /
private class AuthResultHandler实现了Firebase.AuthResultHandler {

private final String提供者;

public AuthResultHandler(String provider){
this.provider = provider;
}

@Override
public void onAuthenticated(AuthData authData){
mAuthProgressDialog.hide();
Log.i(TAG,provider +auth successful);
setAuthenticatedUser(authData);

$ b $ @覆盖
public void onAuthenticationError(FirebaseError firebaseError){
mAuthProgressDialog.hide();
showErrorDialog(firebaseError.toString());
}
}

/ * ****************************** ******
*密码*
********************************* *****
* /
public void loginWithPassword(){
mAuthProgressDialog.show();
mFirebaseRef.authWithPassword(test@firebaseuser.com,test1234,new AuthResultHandler(password));
}

/ * *********************************** *
* ANONYMOUSLY *
**************************************
* /
private void loginAnonymously(){
mAuthProgressDialog.show();
mFirebaseRef.authAnonymously(new AuthResultHandler(anonymous));



$ b public void switchToRegister(View v){
Intent Register = new Intent(this,Register.class);
startActivity(Register);


$ b $ / code $ / pre

声明该类为公共和扩展AppCompatActivity(不知道我刚刚从另一个教程中学到了什么,我也试图扩展它从应用程序,我认为可以工作,但随后各种错误在列出的代码:






$ p $ <?xml version = 1.0encoding =utf-8?>

 < uses-permission android:name =android.permission.INTERNET/> 
<使用权限android:name =android.permission.ACCESS_NETWORK_STATE/>
<使用权限android:name =android.permission.ACCESS_WIFI_STATE/>
< uses-permission android:name =android.permission.GET_ACCOUNTS/>
< uses-permission android:name =android.permission.USE_CREDENTIALS/>

应用程序$ b $ android:allowBackup =true
android:icon =@ mipmap / ic_launcher
android:label =@ string / app_name
android:supportsRtl =true
android:theme =@ style / AppTheme>
android:name =net.we4x4.we4x4.MainActivity
android:label =@ string / app_name>
< intent-filter>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>

< activity android:name =。LoginFragment>
< / activity>

< activity android:name =。Register>
< / activity>

<! - 注意:这是自动生成的,可以将Google Play服务添加到您的项目中,作为
App Indexing。有关更多信息,请参阅https://g.co/AppIndexing/AndroidStudio。 - >
< meta-data
android:name =com.google.android.gms.version
android:value =@ integer / google_play_services_version/>
< / application>



在第27行下.LoginFragment它说它不能分配给android.app.activity?

然后在代码本身LoginFragment:



我得到这些我不能解决的错误:
第58行 - setContentView不能解决这个方法?
setContentView(R.layout.fragment_login);

第63行findViewById也不能解析这个方法?

pre> mPasswordLoginButton =(Button)findViewById(R.id.login_with_password);

同一个匿名选项75
mAnonymousLoginButton =(Button)findViewById(R.id。 login_anonymously);

第86行findViewById同样的问题?
mLoggedInStatusTextView =(TextView)findViewById(R.id.login_status);

显然我需要创建findViewById mehtod如果我想扩展应用程序,但我做了不要继续,因为我不知道我是否应该从应用程序扩展,考虑到我没有得到这些代码中的任何错误,当我从MainActivity或AppCompatActivity延伸



还有我得到行114不能解决这个方法
super.onDestroy();

和其他几个....?$ / b
$ b

所以我不知道如何解决这个问题?我应该扩展应用程序和解决这些错误(以及如何解决这些错误),或保持MainActivity或AppCompatActivity的扩展?在代码中不包含错误,但不能正常工作切换活动:

我的MainActivity是:

  package net.we4x4.we4x4; 

导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v7.app.AppCompatActivity;
导入android.view.View;
导入android.widget.MediaController;
导入android.widget.VideoView;

import com.firebase.client.Firebase;

public class MainActivity extends AppCompatActivity {
public static final String VID_URL =http://res.cloudinary.com/wi4x4/video/upload/v1457270321/bolivia_njb6r0.mp4;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
Firebase.setAndroidContext(this);


VideoView vid =(VideoView)findViewById(R。id.VidClip);
Uri video = Uri.parse(VID_URL);
vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.requestFocus();





public void switchToLoginFragment(View v){
Intent LoginFragment = new Intent(this, LoginFragment.class);
startActivity(LoginFragment);
}
public void switchToRegister(View v){
Intent Register = new Intent(this,Register.class);
startActivity(Register);

code










$ b $ p


$ b

错误日志是:

  6:16:51 PM AssertionError:错误的元素范围PsiJavaFile:LoginFragment.java; commit = true 

android监视器:

  03-07 18:19:52.512 21730-21730 / net.we4x4.we4x4 E / AndroidRuntime:致命例外:main 
过程:net.we4x4.we4x4,PID:21730
java.lang.RuntimeException:无法启动活动ComponentInfo {net.we4x4.we4x4 / net.we4x4.we4x4.LoginFragment}:java.lang.IllegalStateException:对于自定义Firebase主机,您必须先使用之前设置您的身份验证服务器认证功能!
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access $ 800(ActivityThread.java:166)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
(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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
导致:java.lang.IllegalStateException:对于自定义的Firebase主机,您必须先使用身份验证功能才能设置您的身份验证服务器!
at com.firebase.client.authentication.AuthenticationManager.checkServerSettings(AuthenticationManager.java:221)
at com.firebase.client.authentication.AuthenticationManager.addAuthStateListener(AuthenticationManager.java:625)
at.firebase.addAuthStateListener(Firebase.java:555)
at net.we4x4.we4x4.LoginFragment.onCreate(LoginFragment.java:107)
at android.app.Activity.performCreate (Activity.java:5447)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
在android .app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access $ 800(ActivityThread.java: 166)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1283)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os .Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)



这个例外:

解决方案

.lang.RuntimeException:无法启动活动ComponentInfo {net.we4x4.we4x4 / net.we4x4.we4x4.we4x4.LoginFragment}:java.lang.IllegalStateException:对于自定义Firebase主机,您必须先使用身份验证功能设置您的身份验证服务器! / p>

来自这行:

m FirebaseRef = new Firebase(getResources()。getString(R.string.firebase_url));

原因是 firebase_url 的格式不是 https://yours.firebaseio.com


I have a problem switching activity when a user would click on a link from MainActivity, the app will just shut down, i am not sure if i have a problem with the project structure or declaring the class correctly and the extends.

Basically what i did is try to implement Firebase example project provided on the website into one activity i have in my app, in order to have a way to let members log in, i striped down the initial code provided here and implemented it in my second activity window, the code in the link is for the MainActivity and i wanted it to be as LoginFragment ( only a name ) didnt use fragments.

thus after minimizing the code ( deleting google, facebook, etc ) options i ended up with this :

package net.we4x4.we4x4;

import android.app.AlertDialog;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;

import java.util.HashMap;
import java.util.Map;

 public class LoginFragment extends AppCompatActivity{

    private static final String TAG = MainActivity.class.getSimpleName();

    /* *************************************
     *              GENERAL                *
     ***************************************/
/* TextView that is used to display information about the logged in user */
    private TextView mLoggedInStatusTextView;

    /* A dialog that is presented until the Firebase authentication finished. */
    private ProgressDialog mAuthProgressDialog;

    /* A reference to the Firebase */
    private Firebase mFirebaseRef;

    /* Data from the authenticated user */
    private AuthData mAuthData;

    /* Listener for Firebase session changes */
    private Firebase.AuthStateListener mAuthStateListener;

    /* *************************************
     *              PASSWORD               *
     ***************************************/
    private Button mPasswordLoginButton;

    /* *************************************
     *            ANONYMOUSLY              *
     ***************************************/
    private Button mAnonymousLoginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    /* Load the view and display it */
        setContentView(R.layout.activity_main);

    /* *************************************
     *               PASSWORD              *
     ***************************************/
        mPasswordLoginButton = (Button) findViewById(R.id.login_with_password);
        mPasswordLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginWithPassword();
            }
        });

    /* *************************************
     *              ANONYMOUSLY            *
     ***************************************/
    /* Load and setup the anonymous login button */
        mAnonymousLoginButton = (Button) findViewById(R.id.login_anonymously);
        mAnonymousLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginAnonymously();
            }
        });

    /* *************************************
     *               GENERAL               *
     ***************************************/
        mLoggedInStatusTextView = (TextView) findViewById(R.id.login_status);

    /* Create the Firebase ref that is used for all authentication with Firebase */
        mFirebaseRef = new Firebase(getResources().getString(R.string.firebase_url));

    /* Setup the progress dialog that is displayed later when authenticating with Firebase */
        mAuthProgressDialog = new ProgressDialog(this);
        mAuthProgressDialog.setTitle("Loading");
        mAuthProgressDialog.setMessage("Authenticating with Firebase...");
        mAuthProgressDialog.setCancelable(false);
        mAuthProgressDialog.show();

        mAuthStateListener = new Firebase.AuthStateListener() {
            @Override
            public void onAuthStateChanged(AuthData authData) {
                mAuthProgressDialog.hide();
                setAuthenticatedUser(authData);
            }
        };
    /* Check if the user is authenticated with Firebase already. If this is the case we can set the authenticated
     * user and hide hide any login buttons */
        mFirebaseRef.addAuthStateListener(mAuthStateListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();


        // if changing configurations, stop tracking firebase session.
        mFirebaseRef.removeAuthStateListener(mAuthStateListener);
    }

    /**
     * This method fires when any startActivityForResult finishes. The requestCode maps to
     * the value passed into startActivityForResult.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Map<String, String> options = new HashMap<String, String>();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    /* If a user is currently authenticated, display a logout menu */
        if (this.mAuthData != null) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_logout) {
            logout();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Unauthenticate from Firebase and from providers where necessary.
     */
    private void logout() {
        if (this.mAuthData != null) {
        /* logout of Firebase */
            mFirebaseRef.unauth();
            }
        /* Update authenticated user and show login buttons */
            setAuthenticatedUser(null);
        }


    /**
     * This method will attempt to authenticate a user to firebase given an oauth_token (and other
     * necessary parameters depending on the provider)
     */
    private void authWithFirebase(final String provider, Map<String, String> options) {
        if (options.containsKey("error")) {
            showErrorDialog(options.get("error"));
        } else {
            mAuthProgressDialog.show();
            if (provider.equals("twitter")) {
                // if the provider is twitter, we pust pass in additional options, so use the options endpoint
                mFirebaseRef.authWithOAuthToken(provider, options, new AuthResultHandler(provider));
            } else {
                // if the provider is not twitter, we just need to pass in the oauth_token
                mFirebaseRef.authWithOAuthToken(provider, options.get("oauth_token"), new AuthResultHandler(provider));
            }
        }
    }

    /**
     * Once a user is logged in, take the mAuthData provided from Firebase and "use" it.
     */
    private void setAuthenticatedUser(AuthData authData) {
        if (authData != null) {
        /* Hide all the login buttons */
            mPasswordLoginButton.setVisibility(View.GONE);
            mAnonymousLoginButton.setVisibility(View.GONE);
            mLoggedInStatusTextView.setVisibility(View.VISIBLE);
        /* show a provider specific status text */
            String name = null;
            if (authData.getProvider().equals("facebook")
                    || authData.getProvider().equals("google")
                    || authData.getProvider().equals("twitter")) {
                name = (String) authData.getProviderData().get("displayName");
            } else if (authData.getProvider().equals("anonymous")
                    || authData.getProvider().equals("password")) {
                name = authData.getUid();
            } else {
                Log.e(TAG, "Invalid provider: " + authData.getProvider());
            }
            if (name != null) {
                mLoggedInStatusTextView.setText("Logged in as " + name + " (" + authData.getProvider() + ")");
            }
        } else {
        /* No authenticated user show all the login buttons */
            mPasswordLoginButton.setVisibility(View.VISIBLE);
            mAnonymousLoginButton.setVisibility(View.VISIBLE);
            mLoggedInStatusTextView.setVisibility(View.GONE);
        }
        this.mAuthData = authData;
    /* invalidate options menu to hide/show the logout button */
        supportInvalidateOptionsMenu();
    }



    /**
     * Show errors to users
     */
    private void showErrorDialog(String message) {
        new AlertDialog.Builder(this)
                .setTitle("Error")
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, null)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    /**
     * Utility class for authentication results
     */
    private class AuthResultHandler implements Firebase.AuthResultHandler {

        private final String provider;

        public AuthResultHandler(String provider) {
            this.provider = provider;
        }

        @Override
        public void onAuthenticated(AuthData authData) {
            mAuthProgressDialog.hide();
            Log.i(TAG, provider + " auth successful");
            setAuthenticatedUser(authData);
        }

        @Override
        public void onAuthenticationError(FirebaseError firebaseError) {
            mAuthProgressDialog.hide();
            showErrorDialog(firebaseError.toString());
        }
    }

    /* ************************************
     *              PASSWORD              *
     **************************************
     */
    public void loginWithPassword() {
        mAuthProgressDialog.show();
        mFirebaseRef.authWithPassword("test@firebaseuser.com", "test1234", new AuthResultHandler("password"));
    }

    /* ************************************
     *             ANONYMOUSLY            *
     **************************************
     */
    private void loginAnonymously() {
        mAuthProgressDialog.show();
        mFirebaseRef.authAnonymously(new AuthResultHandler("anonymous"));
    }



public void switchToRegister(View v) {
    Intent Register = new Intent(this, Register.class);
    startActivity(Register);
}

}

and as you would guys see i declared the class as public and extended AppCompatActivity ( not sure what i had to extend just learned that from another tutorial ) i also tried to extend it from Application, which i thought could work, but then all sort of errors poped up in the code listed :

in the manifesto :

 <?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name="net.we4x4.we4x4.MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".LoginFragment">
    </activity>

    <activity android:name=".Register">
    </activity>

    <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
         App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

in line 27 under .LoginFragment it says that it is not assignable to android.app.activity ?

and then in the code itself LoginFragment :

i get these errors which i could not solve : line 58 - setContentView cant resolve this method ? setContentView(R.layout.fragment_login);

line 63 findViewById also cant resolve this method ?

         mPasswordLoginButton = (Button) findViewById(R.id.login_with_password);

the same anonymous option line 75 mAnonymousLoginButton = (Button) findViewById(R.id.login_anonymously);

the same problem with line 86 findViewById ? mLoggedInStatusTextView = (TextView) findViewById(R.id.login_status);

Apparently i need to create findViewById mehtod if i want to extends Application, but i did not proceed with this since i do not know if i should extends from Application, considering i do not get any of these errors in the code when i extends from MainActivity or AppCompatActivity

also i get on line 114 cant resolve this method super.onDestroy();

and few others .... ?

so i am not sure how to solve this problem ? should i extends Application and tackle these errors ( and how to solve these errrors ) or keep the extend of MainActivity or AppCompatActivity ? which contain no error in the code but will not function properly switching activity:

my MainActivity is :

 package net.we4x4.we4x4;

import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.MediaController;
 import android.widget.VideoView;

 import com.firebase.client.Firebase;

 public class MainActivity extends AppCompatActivity {
public static final String VID_URL = "http://res.cloudinary.com/wi4x4/video/upload/v1457270321/bolivia_njb6r0.mp4";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        Firebase.setAndroidContext(this);


    VideoView vid = (VideoView)findViewById(R. id.VidClip);
    Uri video = Uri.parse(VID_URL);
    vid.setMediaController(new MediaController(this));
    vid.setVideoURI(video);
    vid.requestFocus();


}
 }


public void switchToLoginFragment(View v) {
    Intent LoginFragment = new Intent(this, LoginFragment.class);
    startActivity(LoginFragment);
}
public void switchToRegister(View v) {
    Intent Register = new Intent(this, Register.class);
    startActivity(Register);
}

}

and the error log is :

 6:16:51 PM AssertionError: Wrong element range PsiJavaFile:LoginFragment.java; committed=true

android monitor :

 03-07 18:19:52.512 21730-21730/net.we4x4.we4x4 E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: net.we4x4.we4x4, PID: 21730
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{net.we4x4.we4x4/net.we4x4.we4x4.LoginFragment}: java.lang.IllegalStateException: For a custom firebase host you must first set your authentication server before using authentication features!
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:166)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:136)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5584)
                                                                 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:1268)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                              Caused by: java.lang.IllegalStateException: For a custom firebase host you must first set your authentication server before using authentication features!
                                                                 at com.firebase.client.authentication.AuthenticationManager.checkServerSettings(AuthenticationManager.java:221)
                                                                 at com.firebase.client.authentication.AuthenticationManager.addAuthStateListener(AuthenticationManager.java:625)
                                                                 at com.firebase.client.Firebase.addAuthStateListener(Firebase.java:555)
                                                                 at net.we4x4.we4x4.LoginFragment.onCreate(LoginFragment.java:107)
                                                                 at android.app.Activity.performCreate(Activity.java:5447)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:136) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5584) 
                                                                 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:1268) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                 at dalvik.system.NativeStart.main(Native Method) 

解决方案

This exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.we4x4.we4x4/net.we4x4.we4x4.LoginFragment}: java.lang.IllegalStateException: For a custom firebase host you must first set your authentication server before using authentication features!

Comes from this line:

mFirebaseRef = new Firebase(getResources().getString(R.string.firebase_url));

The reason is that firebase_url is not in the form https://yours.firebaseio.com.

这篇关于切换到firebase-应用程序的活动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆