我目前正在为决赛编写一个应用程序,但似乎无法启动我的一项活动。假设它是一个recyclerView,它将允许用户为自己创建锻炼清单。但是,当我去启动活动时,程序崩溃了。我的错误代码询问我是否已在清单中声明了活动,而我已经这样做了,我想我虽然没有正确执行某些操作,但是我不知道自己做了什么。我进行了搜索,但似乎我的问题很少见,可能是因为它很简单,而且我自己遇到了用户错误。感谢您的任何帮助,谢谢。
我的常规寻呼机活动:
package hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.RoutineList;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.List;
import java.util.UUID;
import hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.R;
public class RoutinePagerActivity extends AppCompatActivity
{
private static final String EXTRA_ROUTINE_ID = "com.bignerdranch.android.routinelist.routine_id";
private ViewPager mViewPager;
private List<Routine> mRoutines;
public static Intent newIntent(Context packageContext, UUID routineId) {
Intent intent = new Intent(packageContext, RoutinePagerActivity.class);
intent.putExtra(EXTRA_ROUTINE_ID, routineId);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routine_pager);
UUID routineId = (UUID) getIntent().getSerializableExtra(EXTRA_ROUTINE_ID);
mViewPager = (ViewPager) findViewById(R.id.routine_view_pager);
mRoutines = RoutineLab.get(this).getRoutines();
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
@Override
public Fragment getItem(int position) {
Routine crime = mRoutines.get(position);
return RoutineFragment.newInstance(crime.getId());
}
@Override
public int getCount() {
return mRoutines.size();
}
});
for (int i = 0; i < mRoutines.size(); i++) {
if (mRoutines.get(i).getId().equals(routineId)) {
mViewPager.setCurrentItem(i);
break;
}
}
}
}
我的例行列表活动:
package hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.RoutineList;
import android.support.v4.app.Fragment;
public class RoutineListActivity extends SingleFragmentActivity
{
@Override
protected Fragment createFragment()
{
return new RoutineListFragment();
}
}
我的常规列表片段(如果需要):
package hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.RoutineList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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;
import android.widget.TextView;
import java.util.List;
import hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.R;
public class RoutineListFragment extends Fragment
{
private static final String SAVED_SUBTITLE_VISIBLE = "subtitle";
private RecyclerView mRoutineRecyclerView;
private RoutineAdapter mAdapter;
private boolean mSubtitleVisible;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_routine_list, container, false);
mRoutineRecyclerView = (RecyclerView) view
.findViewById(R.id.routine_recycler_view);
mRoutineRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
if(savedInstanceState != null)
{
mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
}
updateUI();
return view;
}
@Override
public void onResume()
{
super.onResume();
updateUI();
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_SUBTITLE_VISIBLE,mSubtitleVisible);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_routine_list, menu);
MenuItem subtitleItem = menu.findItem(R.id.show_subtitle);
if (mSubtitleVisible)
{
subtitleItem.setTitle(R.string.hide_subtitle);
} else {
subtitleItem.setTitle(R.string.show_subtitle);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.new_routine:
Routine routine = new Routine();
RoutineLab.get(getActivity()).addRoutine(routine);
Intent intent = RoutinePagerActivity
.newIntent(getActivity(), routine.getId());
startActivity(intent);
return true;
case R.id.show_subtitle:
mSubtitleVisible = !mSubtitleVisible;
getActivity().invalidateOptionsMenu();
updateSubtitle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void updateSubtitle() {
RoutineLab routineLab = RoutineLab.get(getActivity());
int routineCount = routineLab.getRoutines().size();
String subtitle = getString(R.string.subtitle_format, routineCount);
if(!mSubtitleVisible)
{subtitle = null;}
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.getSupportActionBar().setSubtitle(subtitle);
}
private void updateUI()
{
RoutineLab routineLab = RoutineLab.get(getActivity());
List<Routine> routines = routineLab.getRoutines();
if (mAdapter == null) {
mAdapter = new RoutineAdapter(routines);
mRoutineRecyclerView.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
updateSubtitle();
}
private class RoutineHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView mTitleTextView;
private TextView mDateTextView;
private ImageView mDoneImageView; //for the handcuffs
private Routine mRoutine;
public RoutineHolder(LayoutInflater inflater, ViewGroup parent)
{
super(inflater.inflate(R.layout.list_item_routine, parent, false));
itemView.setOnClickListener(this);
mTitleTextView = itemView.findViewById(R.id.routine_title);
mDateTextView = itemView.findViewById(R.id.routine_date);
mDoneImageView = itemView.findViewById(R.id.routine_completed); //finds the handcuffs
}
public void bind(Routine routine)
{
mRoutine = routine;
mTitleTextView.setText(mRoutine.getTitle());
mDateTextView.setText(mRoutine.getDate().toString());
mDoneImageView.setVisibility(routine.isDone() ? View.VISIBLE:View.GONE); // handcuffs on every other row
}
@Override
public void onClick(View view)
{
// Toast.makeText(getActivity(), mRoutine.getTitle() + "clicked ", Toast.LENGTH_SHORT).show(); hold this code, I want a toast when clicked as well
Intent intent = RoutinePagerActivity.newIntent(getActivity(),mRoutine.getId());
startActivity(intent);
}
}
private class RoutineAdapter extends RecyclerView.Adapter<RoutineHolder>
{
private List<Routine> mRoutines;
public RoutineAdapter(List<Routine> routines)
{
mRoutines = routines;
}
@Override
public RoutineHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new RoutineHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(RoutineHolder holder, int position)
{
Routine routine = mRoutines.get(position);
holder.bind(routine);
}
@Override
public int getItemCount()
{
return mRoutines.size();
}
}
}
活动开始时的我的错误代码:
12-12 19:03:02.301 23200-23200/hbratcher1992.buccaneer.atlantic.edu.fightingforfitness E/AndroidRuntime: FATAL EXCEPTION: main
Process: hbratcher1992.buccaneer.atlantic.edu.fightingforfitness, PID: 23200
android.content.ActivityNotFoundException: Unable to find explicit activity class {hbratcher1992.buccaneer.atlantic.edu.fightingforfitness/hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.RoutineList.RoutineListActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:732)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.MainMenu$1.onClick(MainMenu.java:45)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main.MainMenu">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main.RoutineList.RoutinePagerActivity"
android:parentActivityName=".Main.RoutineList.RoutineListActivity"/>
<activity android:name=".Main.ULinks.UsefulLinks" />
<activity android:name=".Main.UInformation.UserInformation" />
<activity android:name=".Main.ULinks.Recipes" />
<activity android:name=".Main.ULinks.YoutubeGuides" />
<activity android:name=".Main.ULinks.Story" />
<activity
android:name=".Main.Notepad.NotePad"
android:label="@string/title_activity_note_pad" />
<activity
android:name=".Main.Notepad.NoteSelect"
android:label="@string/title_activity_note_select">
</activity>
</application>