在另一个类中调用AppCompatActivity类

时间:2016-06-07 09:20:04

标签: java android appcompatactivity

我有一个扩展AppCompatActivity的类允许我播放视频(launchVideo方法),我想在类扩展AppCompatActivity中调用。

以下是我的课程,供我阅读视频:

public class ReadVideo extends AppCompatActivity {

// Lien vers la documentation de MX Video Player ==> https://sites.google.com/site/mxvpen/api
public static final String MXVP = "com.mxtech.videoplayer.ad";

public static final String EXTRA_VIDEO_LIST = "video_list";
public static final String EXTRA_POSITION = "position";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void launchVideo(View v) {
    try{
        // Chemin de la vidéo
        Uri videoUri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Movies/chrono.mp4");

        // Nouvelle activité qui permet de visionner une donnée (dans notre cas, la vidéo)
        Intent intent = new Intent(Intent.ACTION_VIEW);

        // On spécifie la donnée de l'activité ainsi que le MIME (ex: application, text, image, audio, etc.)
        // On veut lancer l'APPLICATION MX Video Player donc le MIME est "application"
        intent.setDataAndType(videoUri, "application/x-mpegURL");

        intent.putExtra(EXTRA_VIDEO_LIST, new Parcelable[] {videoUri});    // Permet d'éviter de lire toutes les vidéos du dossier du chemin envoyé
        intent.putExtra(EXTRA_POSITION, 10000);    // Commence la vidéo à 10sec

        //intent.setPackage(MXVP);    // Limite les applications possibles à celle de MX Video Player
        startActivity(intent);
    }
    catch( ActivityNotFoundException e2){
        displayToast(getResources().getString(R.string.error_unknownMX)); // Erreur, on affiche un message à l'utilisateur
        Log.e( "Error", getResources().getString(R.string.error_unknownMX));
    }
}

我想打电话的课程:

public class SimpleTextClientActivity extends AppCompatActivity {

private EditText textField;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textField = (EditText) findViewById(R.id.editText1); // reference to the text field
    button = (Button) findViewById(R.id.button); // reference to the send button

    // Button press event listener
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //messsage = textField.getText().toString(); // get the text message on the text field
           // textField.setText(""); // Reset the text field to blank
            ReadVideo task2 = new ReadVideo();
            task2.launchVideo(v);
        }
    });
}

2 个答案:

答案 0 :(得分:0)

你必须使用Intent

Intent videoIntent=new Intent(SimpleTextClientActivity .this,ReadVideo.class);
startActivity(videoIntent);

在ReadVideo活动 onCreate()

中调用 launchVideo()方法

答案 1 :(得分:0)

现在我在我的班级SimpleTextClientActivity

上这样做
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textField = (EditText) findViewById(R.id.editText1); // reference to the text field
    button = (Button) findViewById(R.id.button); // reference to the send button

    // Button press event listener
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //messsage = textField.getText().toString(); // get the text message on the text field
           // textField.setText(""); // Reset the text field to blank
            Intent videoIntent=new Intent(SimpleTextClientActivity .this,ReadVideo.class);
            startActivity(videoIntent);


        }
    });
}

在我的课程上阅读视频:

 View v;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    launchVideo(v);
}

但是当我点击我的按钮时,应用程序崩溃了。