gpt4 book ai didi

java - 无法启动 Activity ComponentInfo | E/Android运行时 : FATAL EXCEPTION

转载 作者:行者123 更新时间:2023-12-03 08:46:30 25 4
gpt4 key购买 nike

问候大家,我正在尝试制作一个带有抽屉导航的应用程序,并且能够在其中一个抽屉中包含可选择的联系人,但它一直在崩溃。我构建的应用程序非常适合显示联系人。当我开始尝试单击联系人以导航到另一个具有联系人信息的 fragment 时,该应用程序将无法启动。非常感谢任何帮助,即使是指出我正确的方向。

Project File

主要 Activity Java fragment

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());
}

@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_activity_faculty) {
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,
new ViewContactsFragment()).commit();

} else if (id == R.id.nav_second_layout) {
getFragmentManager().beginTransaction().replace(R.id.content_frame,
new SecondFragment()).commit();

} else if (id == R.id.nav_third_layout) {
getFragmentManager().beginTransaction().replace(R.id.content_frame,
new ThirdFragment()).commit();

} else if (id == R.id.nav_fourth_layout) {
getFragmentManager().beginTransaction().replace(R.id.content_frame,
new FourthFragment()).commit();

} else if (id == R.id.nav_fifth_layout) {
getFragmentManager().beginTransaction().replace(R.id.content_frame,
new FifthFragment()).commit();

} else if (id == R.id.nav_sixth_layout) {
getFragmentManager().beginTransaction().replace(R.id.content_frame,
new SixthFragment()).commit();

}



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

}
private void init()
{
{
ViewContactsFragment fragment = new ViewContactsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}




}

我的联系人 fragment
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ContactFragment extends Fragment {
private static final String TAG = "ContactFragment";

public ContactFragment(){
super();
setArguments(new Bundle());
}

private Toolbar toolbar;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact, container, false);
toolbar = (Toolbar) view.findViewById(R.id.contactToolbar);
Log.d(TAG, "onCreateView: started");

//Toolbar Pre-req//
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
setHasOptionsMenu(true);

//Go Back//
ImageView ivBackArrow = (ImageView) view.findViewById(R.id.ivBackArrow);
ivBackArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked back arrow");
getActivity().getSupportFragmentManager().popBackStack();
}
});
//Go To Edit//
ImageView ivEdit = (ImageView) view.findViewById(R.id.ivEdit);
ivEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked edit");
EditContactFragment fragment = new EditContactFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(getString(R.string.edit_contact_fragment));
Log.d(TAG, "onClick: fragment" + getString(R.string.edit_contact_fragment));
transaction.commit();
}
});

return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.contact_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId())
{
case R.id.menuitem_delete:
Log.d(TAG, "onOptionsItemSelected: deleting contact");
}
return super.onOptionsItemSelected(item);
}
}

还有包含联系信息的 View Contact Fragment
import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

import com.example.v1z0.assignments.Models.Contact;
import com.example.v1z0.assignments.Utils.CustomListAdapter;


public class ViewContactsFragment extends Fragment {

private static final String TAG = "FirstFragment";

//images
private String testImageURL = "cis.ncu.edu.jm/images/faculty/man.jpg";
private String hgraham = "cis.ncu.edu.jm/images/faculty/hgraham.jpg";
private String dmitchell = "cis.ncu.edu.jm/images/faculty/dmitchell.png";
private String mnarayana = "cis.ncu.edu.jm/images/faculty/mnarayana.jpg";
private String hosborne = "cis.ncu.edu.jm/images/faculty/hosborne.jpg";
private String mreid = "cis.ncu.edu.jm/images/faculty/mreid.jpg";
private String esmall = "cis.ncu.edu.jm/images/faculty/esmall.jpg";
private String hsmith = "cis.ncu.edu.jm/images/faculty/hsmith.jpg";
private String ktooma = "cis.ncu.edu.jm/images/faculty/ktooma.jpg";

//widgets etc
private static final int STANDARD_APPBAR = 0;
private static final int SEARCH_APPBAR = 1;
private int mAppBarState;

private AppBarLayout viewContactsBar, searchBar;
private CustomListAdapter adapter;
private ListView contactsList;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewcontacts, container, false);
viewContactsBar = (AppBarLayout) view.findViewById(R.id.viewContactsToolbar);
searchBar = (AppBarLayout) view.findViewById(R.id.searchtoolbar);
contactsList = (ListView) view.findViewById(R.id.contactsList);
Log.d(TAG, "onCreate: started");

setAppBarState(STANDARD_APPBAR);
setupContactsList();

//add contact navigation//
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fabAddContact);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked fab");
}

});

//search contact navigation//
ImageView ivSearchContact = (ImageView) view.findViewById(R.id.ivSearchIcon);
ivSearchContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked search");
toggleToolBarState();
}
});

ImageView ivBackArrow = (ImageView) view.findViewById(R.id.ivBackArrow);
ivBackArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked back arrow");
toggleToolBarState();
}
});

return view;
}


private void setupContactsList()
{
final ArrayList<Contact> contacts = new ArrayList<>();
contacts.add(new Contact("Mr. Sheldon Glenn ", "(876) 963-7281", "work", "sheldon.glenn@ncu.edu.jm ", testImageURL));
contacts.add(new Contact("Mr. Hamlet Graham ", "(876) 963-7278", "work", "hamlet.graham@ncu.edu.jm ", hgraham));
contacts.add(new Contact("Mr. Oche Magbegor ", "(876) 963-7280", "work", "oche.magbegor@ncu.edu.jm ", testImageURL));
contacts.add(new Contact("Mr. Damion Mitchell ", "(876) 963-7285", "work", "damion.mitchell AT ncu.edu.jm ", dmitchell));
contacts.add(new Contact("Mr. Melvin Narayana ", "(876) 963-7279", "work", "melvin.narayana@ncu.edu.jm", mnarayana));
contacts.add(new Contact("Mr. Henry Osborne ", "(876) 963-7282", "work", "Henry.Osborne@ncu.edu.jm ", hosborne));
contacts.add(new Contact("Mr. Marlon Richards ", " (876) 963-7276", "work", "marlon.richards@ncu.edu.jm", testImageURL));
contacts.add(new Contact("Mrs. E. Beverley Small ", "(876) 963-7276", "work", "esmall@ncu.edu.jm ", esmall));
contacts.add(new Contact("Mr. Halzen Smith ", "(876) 963-7284", "work", "hsmith@ncu.edu.jm ", hsmith));
contacts.add(new Contact("Mr. Keron Tooma ", " (876) 963-7283", "work", "keron.tooma@ncu.edu.jm ", ktooma));

adapter = new CustomListAdapter(getActivity(), R.layout.layout_contactslistitem, contacts, "http://");
contactsList.setAdapter(adapter);

contactsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
Log.d(TAG, "onClick: navigating to contact" + (R.string.contact_fragment));
ContactFragment fragment = new ContactFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(getString(R.string.edit_contact_fragment));
Log.d(TAG, "onClick: fragment" + getString(R.string.contact_fragment));
transaction.commit();
}
});

}

private void toggleToolBarState()
{
Log.d(TAG, "toggleToolBarState: toggling AppBatState");
if (mAppBarState == STANDARD_APPBAR)
{
setAppBarState(SEARCH_APPBAR);
}
else
{
setAppBarState(STANDARD_APPBAR);
}
}

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

private void setAppBarState(int state)
{
Log.d(TAG, "setAppBarState: changing app bar state to" + state);

mAppBarState = state;

if (mAppBarState == STANDARD_APPBAR)
{
searchBar.setVisibility(View.GONE);
viewContactsBar.setVisibility(View.VISIBLE);
//keyboard hide//
View view = getView();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
try
{
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

}catch (NullPointerException e)
{
Log.d(TAG, "setAppBarState: NullPointerException" + e.getMessage());
}
}
else if (mAppBarState == SEARCH_APPBAR)
{
viewContactsBar.setVisibility(View.GONE);
searchBar.setVisibility(View.VISIBLE);
//keyboard show//
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}



}
}

LogCat 错误
  --------- beginning of crash
2018-10-14 01:48:19.869 7041-7041/com.example.v1z0.assignments E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.v1z0.assignments, PID: 7041
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.v1z0.assignments/com.example.v1z0.assignments.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080055 (com.example.v1z0.assignments:id/fragment_container) for fragment ViewContactsFragment{dcf4537 #0 id=0x7f080055}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080055 (com.example.v1z0.assignments:id/fragment_container) for fragment ViewContactsFragment{dcf4537 #0 id=0x7f080055}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1422)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at android.app.Activity.performStart(Activity.java:7029)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

XML fragment

独奏联系页面
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout1">

<include layout="@layout/snippet_contacttoolbar"/>

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relLayout1"
android:id="@+id/relLayout2">

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
app:civ_border_color="@color/black"
app:civ_border_width="1dp"
android:id="@+id/contactImage" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/contactImage"
android:layout_marginTop="30dp"
android:textSize="30sp"
android:textColor="@color/black"
android:layout_marginStart="30dp"
android:id="@+id/tvName" />

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tvName"
android:id="@+id/lvContactProperties">
</ListView>

</RelativeLayout>


</RelativeLayout>


</android.support.design.widget.CoordinatorLayout>

查看联系人列表 fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/first">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relLayout1">

<include layout="@layout/snippet_searchtoolbar"/>
<include layout="@layout/snippet_viewcontactstoolbar"/>

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relLayout1">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/contactsList"></ListView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:id="@+id/textNoContacts"
android:textColor="@color/black"
/>

</RelativeLayout>

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:elevation="10dp"
app:fabSize="normal"
app:rippleColor="@color/colorAccent"
android:src="@drawable/ic_person_add"
android:id="@+id/fabAddContact"/>

</android.support.design.widget.CoordinatorLayout>

编辑页面
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.constraint.ConstraintLayout>

我为这个冗长的问题道歉

最佳答案

在您的 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();
}
}

关于java - 无法启动 Activity ComponentInfo | E/Android运行时 : FATAL EXCEPTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com