如何从片段中调用类或活动?

时间:2019-09-23 14:58:24

标签: android android-fragments

我正在处理tabbed_fragments活动。现在,我想在片段数据上显示一些活动,包括文本,按钮,单选按钮等。为此,我分别制作了该类的class + xml文件,它完美地独立工作。但是,当我调用该片段时,应用程序崩溃了。为了解决这个问题,我已经搜索了互联网,但是对我来说却不起作用。我使用的一种解决方案,您可以从下面的给定链接中找到:

how to call Activity class from Fragment

我的片段代码:


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

//import com.example.salmakhalil.toolbar_2.R;

    public class frag1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Intent i = new Intent(getActivity(), verbalquestionsActivity.class);
        startActivity(i);
        View view = inflater.inflate(R.layout.fragment1_layout, container, false);
      return view

    }





}

日志文件

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.salmakhalil.toolbar_2, PID: 4284
                  android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.salmakhalil.toolbar_2/com.example.salmakhalil.toolbar_2.verbalquestionsActivity}; 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.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
                      at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
                      at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:881)
                      at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:995)
                      at android.support.v4.app.Fragment.startActivity(Fragment.java:1084)
                      at android.support.v4.app.Fragment.startActivity(Fragment.java:1073)
                      at com.example.salmakhalil.toolbar_2.frag1.onCreateView(frag1.java:31)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
                      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:733)
                      at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:95)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1556)
                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:803)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                      at android.view.View.measure(View.java:17547)
                    at android.v
I/Process: Sending signal. PID: 4284 SIG: 9
Application terminated.

1 个答案:

答案 0 :(得分:1)

实际上,我将一个活动代码复制并粘贴到我的当前项目中,但是忘记了将此活动声明到清单文件中。 @Agnarmon从日志文件中突出显示了此问题。因此,我只是使用以下代码声明了活动:<activity android:name=".verbalquestionsActivity"></activity>

及其完美的效果。非常感谢@Agnarmon。

此外,我编写了以下代码以从片段中调用活动:


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

//import com.example.salmakhalil.toolbar_2.R;

    public class frag1 extends Fragment{
        Context context; //Declare the variable context

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            //Pass your layout xml to the inflater and assign it to rootView.
            View rootView = inflater.inflate(R.layout.fragment1_layout, container, false);
            context = rootView.getContext(); // Assign your rootView to context
          Intent intent = new Intent(context, verbalquestionsActivity.class);
        startActivity(intent);

            return rootView;
        }

} 

它工作得很好...