我正在尝试从活动导航到片段,但我遇到了错误。我已经发布了完整的代码。请帮我解决这个问题。欢迎所有建议。请在下面找到我的代码:
EditScreen.java:
public class EditScreen extends ActionBarActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_screen);
toolbar = (Toolbar) findViewById(R.id.appBar);
setSupportActionBar(toolbar);
}
public void update (View view) {
Intent intent = new Intent(this, DetailViewFragmentActivity.class);
startActivity(intent);
}
}
DetailViewFragment.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class DetailViewFragment extends Fragment {
public DetailViewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detail_view, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
DetailViewFragmentActivity.java:
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class DetailViewFragmentActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_detail_view);
DetailViewFragment f1 = new DetailViewFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
f1.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_detail, f1).commit();
}
}
fragment_detail_view.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ms.t.tms.DetailViewFragment"
android:background="@color/colorPrimary">
<include
android:id="@+id/appBar"
layout="@layout/app_bar" ></include>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/update"
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="@color/textColor"
android:onClick="update"/>
</RelativeLayout>
LogCat追踪:
02-16 15:08:41.492 965-965/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ms.t.tms, PID: 965
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.ms.t.tms/com.ms.t.tms.DetailViewFragmentActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:4007)
at android.app.Activity.startActivity(Activity.java:3975)
at com.ms.t.tms.EditScreen.update(EditScreen.java:65)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ms.t.tms" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EditScreen"
android:label="@string/title_activity_edit_screen" >
</activity>
<activity
android:name=".DetailViewFragActivity"
android:label="@string/title_activity_detail_view_frag" >
</activity>
</application>
</manifest>
当我从EditScreen中选择Update方法时,我希望导航到详细视图,这是一个片段。当我选择更新按钮时,我收到上面发布的错误。请让我知道我的错误。提前致谢。 如果需要更多信息,请告诉我。
答案 0 :(得分:2)
你应该改变
android:name=".DetailViewFragActivity"
到
android:name=".DetailViewFragmentActivity"
检查活动名称。这是DetailViewFragmentActivity
而不是DetailViewFragActivity