无法启动活动 ComponentInfo | E/AndroidRuntime:致命异常

大家好,我正在尝试制作一个带有导航抽屉的应用程序,并且能够在其中一个抽屉中拥有可选择的联系人,但它一直崩溃。我构建的应用程序非常适合显示联系人。当我开始尝试实现点击联系人以导航到另一个包含联系信息的 Fragment 时,应用程序无法启动。非常感谢任何帮助,甚至为我指明正确的方向。

主要活动 Java 片段


import android.support.v4.app.FragmentTransaction;

import android.os.Bundle;

import android.support.design.widget.NavigationView;

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.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import com.nostra13.universalimageloader.core.ImageLoader;

import com.example.v1z0.assignments.Utils.*;


public class MainActivity extends AppCompatActivity

        implements NavigationView.OnNavigationItemSelectedListener {


    private static final String TAG = "MainActivity";


    @Override

    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);



        Log.d(TAG, "onCreate: started");


        initImageLoader();


        init();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);



        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.addDrawerListener(toggle);

        toggle.syncState();


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

        navigationView.setNavigationItemSelectedListener(this);

    }


    private void initImageLoader()

    {

        UniversalImageLoader universalImageLoader = new UniversalImageLoader(MainActivity.this);

        ImageLoader.getInstance().init(universalImageLoader.getConfig());

    }


守候你守候我
浏览 180回答 2
2回答

慕丝7291255

在init()MainActivity 类文件的方法中,您引用了错误的资源 ID。    private void init()    {        {            ViewContactsFragment fragment = new ViewContactsFragment();            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();            transaction.replace(R.id.content_frame, fragment);//not R.id.fragment_container            transaction.addToBackStack(null);            transaction.commit();        }    }

绝地无双

除了navylover的回答。我还在 ViewContactFragment 中更改了 Fragment_container 并且它起作用了contactsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "onClick: navigating to contact" + (R.string.contact_fragment));&nbsp; &nbsp; &nbsp; &nbsp; ContactFragment fragment = new ContactFragment();&nbsp; &nbsp; &nbsp; &nbsp; FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();&nbsp; &nbsp; &nbsp; &nbsp; transaction.replace(R.id.content_frame, fragment);&nbsp; &nbsp; &nbsp; &nbsp; transaction.addToBackStack(getString(R.string.edit_contact_fragment));&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "onClick: fragment contentFrame" );&nbsp; &nbsp; &nbsp; &nbsp; transaction.commit();&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java