片段,我做错了什么?

时间:2016-02-10 16:30:36

标签: java android android-fragments

所以我正在做this教程所说的。 我有一个带导航抽屉的MainActivity。

这是我的MainActivity.java

package nl.broekhin.magizon;


import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import layout.LoginFragment;
import layout.RoosterFragment;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);




        LoginFragment loginFragment = new LoginFragment();
        RoosterFragment roosterFragment = new RoosterFragment();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, loginFragment).commit();
    }



    public interface OnFragmentInteractionListener {
        //public void onFragmentMessage(String TAG, Object data);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


}

这是我的content_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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="nl.broekhin.magizon.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

然而,当我尝试启动它时,我收到错误。

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {nl.broekhin.magizon / nl.broekhin.magizon.MainActivity}:   java.lang.RuntimeException:nl.broekhin.magizon.MainActivity@3e4143c   必须实现OnFragmentInteractionListener

5 个答案:

答案 0 :(得分:0)

您是否在某处添加了一段代码来检查MainActivity是否是OnFragmentInteractionListener的实例?

如果要在Activity和Fragment之间进行通信,您的活动需要实现Fragment定义为require的接口。

在您的类定义后的MainActivity中的情况:

Class MainActivity extends Activity {

你应该以这种方式实现你的OnFragmentInteractionListener接口:

Class MainActivity extends Activity implements OnFragmentInteractionListener {

实现界面后,您需要实现界面定义的方法,IDE将指导您如何操作。

答案 1 :(得分:0)

在您的LoginFragment中,您正在进行以下投射,

(OnFragmentInteractionListener)getActivity()

但是,您的MainActivity不会实现OnFragmentInteractionListener接口(即使您已定义它)。解决方案很简单。变化,

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener,OnFragmentInteractionListener {

答案 2 :(得分:0)

您可以发布您的登录片段吗?

我想问题是您在活动中夸大了错误的布局文件。请检查膨胀的文件。在您的活动中,它表示R.layout.activity_main,但您将xml命名为content_main.xml

答案 3 :(得分:0)

我解决了!

显然,我必须使用android.support.v4.app.Fragment;而不是android.app.Fragment;。 这是我使用它的代码:

protected void setFragment(Fragment fragment) {
    android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.replace(R.id.fragment_container, fragment);
    t.commit();
}

要设置它(从导航栏onNavigationItemSelected(),您可以执行此操作:

setFragment(new RoosterFragment());

我希望这可以帮助其他人解决同样令人沮丧的问题。

答案 4 :(得分:-1)

我猜你必须在你的活动中实现OnFragmentIntractionListner接口..