8

我在活动中使用片段。我正在使用MediaRecorder进行录音。 我有两部分的活动。 1st本身将列出录制文件的活动。 在右侧,当选择记录新文件时,会调用AudioRecording Activity。 当选择任何列出的文件时,我使用AudioPlayer播放录制的文件。 我在这里能够将活动转换成片段,但当我按下停止它终止应用程序。Android:从片段调用活动

任何人都可以回答。当我使用它作为简单的活动时,我的audiorecorder工作正常。 任何解决方案,如果我可以在该片段或类似的东西中调用该活动。 如果有人知道,请帮助我。

Intent i = new Intent(getActivity(), Activity.class); 
startActivity(i); 
+0

从片段调用另一个活动使用likethis: –

回答

3

fragment使用这个调用另一个activity

Intent myIntent = new Intent(getActivity(), BookmarkActivity.class); 
getActivity().startActivity(myIntent); 
28

使用get活动然后做像往常一样获取父活动:

8

这是另一种替代方法。这对我有效。

public class **YourFragmentClass** 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.**yourfragmentxml**, container, false); 
      context = rootView.getContext(); // Assign your rootView to context 

      Button **yourButton** = (Button) rootView.findViewById(R.id.**your_button_id**); 
      **yourButton**.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //Pass the context and the Activity class you need to open from the Fragment Class, to the Intent 
        Intent intent = new Intent(context, **YourActivityClass**.class); 
        startActivity(intent); 
       } 
      }); 
      return rootView; 
     } 
    } 
+0

非常感谢,这正是我正在寻找...;) –

0

你的片断应该有一个父

Intent intent = new Intent(getActivity(), SecondActivity.class); 
getActivity().startActivity(intent); 
1

在片段类

getActivity().startActivity(new Intent(gwtActivity(),MainActivity.class)); 
getActivity().finish(); 
0

最佳呼叫从片段类活动是使界面片段,并添加onItemClick()方法来在那个界面中。现在执行它到你的第一个活动,并从那里调用第二个活动。