android.content.ActivityNotFoundException:无法找到显式活动类

时间:2015-01-30 21:22:51

标签: java android xml facebook android-studio

我已经在互联网上寻找答案,我读过的所有内容都说我的代码是正确的。希望我只是缺少一些东西,另一组眼睛将能够看到我做错了什么。我正在尝试使用后台屏幕和Facebook登录来启动活动。成功登录后,应用程序将启动主要活动。

这是XML文件:     

<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
    <activity
        android:name=".FBActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    <activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
    <activity android:name="com.example.joe.testapp.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

    </activity>

</application>

FBActivity文件。我在onSessionStateChange方法中调用主要活动。

package com.example.joe.testapp;

import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.LoginButton.UserInfoChangedCallback;

public class FBActivity extends FragmentActivity {

private LoginButton loginBtn;
private static final String TAG = "FBActivity";
Intent menuIntent = new Intent(this, MainActivity.class);

private TextView userName;

private UiLifecycleHelper uiHelper;

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");

private static String message = "Sample status posted from android app";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);

    setContentView(R.layout.activity_facebook);

    userName = (TextView) findViewById(R.id.user_name);
    loginBtn = (LoginButton) findViewById(R.id.authButton);
    loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
        @Override
        public void onUserInfoFetched(GraphUser user) {
            if (user != null) {
                userName.setText("Hello, " + user.getName());

            } else {
                userName.setText("You are not logged");
            }
        }
    });

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_facebook, container, false);
}


private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
        startActivity(menuIntent);
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}


private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

public boolean checkPermissions() {
    Session s = Session.getActiveSession();
    if (s != null) {
        return s.getPermissions().contains("publish_actions");
    } else
        return false;
}

public void requestPermissions() {
    Session s = Session.getActiveSession();
    if (s != null)
        s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
                this, PERMISSIONS));
}

@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onSaveInstanceState(Bundle savedState) {
    super.onSaveInstanceState(savedState);
    uiHelper.onSaveInstanceState(savedState);
}

}

和MainActivity文件

package com.example.joe.testapp

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import java.util.List;
import java.util.Vector;


public class MainActivity extends ActionBarActivity {



private Toolbar mainToolBar;
private ImagePagerAdapter mPagerAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    mainToolBar= (Toolbar) findViewById(R.id.main_toolbar);
    setSupportActionBar(mainToolBar);

    TabHost tabs=(TabHost)findViewById(R.id.tabHost);

    tabs.setup();

    TabHost.TabSpec spec=tabs.newTabSpec("tag1");

    spec.setContent(R.id.browse);
    spec.setIndicator("browse");
    tabs.addTab(spec);

    spec=tabs.newTabSpec("tag2");
    spec.setContent(R.id.dailies);
    spec.setIndicator("dailies");
    tabs.addTab(spec);

    spec=tabs.newTabSpec("tag3");
    spec.setContent(R.id.profile);
    spec.setIndicator("profile");
    tabs.addTab(spec);

    allowImageSwiping();


}
private void allowImageSwiping(){
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this,ImageFragment.class.getName()));
    fragments.add(Fragment.instantiate(this,ImageFragment2.class.getName()));
    fragments.add(Fragment.instantiate(this,ImageFragment3.class.getName()));
    fragments.add(Fragment.instantiate(this,ImageFragment4.class.getName()));
    fragments.add(Fragment.instantiate(this,ImageFragment5.class.getName()));

    mPagerAdapter =new ImagePagerAdapter(this.getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager) findViewById(R.id.gallery_item);
    pager.setAdapter(mPagerAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:0)

我在我的应用程序上遇到了类似的问题,但是我阅读了很多有关的信息,并且我看到如果你有应用程序的差异API,你必须更改清单代码,因为有一个错误。

尝试改变这一点:

<activity android:name="com.example.joe.testapp.MainActivity"

对于此

<activity android:name=".MainActivity"