2016-06-07 75 views
0

我有一個擴展AppCompatActivity的類允許我播放視頻(launchVideo方法),並且我想在一個類中調用extends AppCompatActivity。在另一個類中調用一個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); 
     } 
    }); 
} 

回答

0

你必須使用意向

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

致電launchVideo()方法裏面ReadVideo活動的onCreate()

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); 


     } 
    }); 
} 

而且在我的類ReadVideo:

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

但是,當我在我的按鈕,單擊該應用程序崩潰。

+0

您是否在清單文件中註冊了您的ReadVideo活動?請檢查安卓logcat – dindinii

+0

將下面的代碼放入您的buttonclick button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Uri videoUri = Uri.parse(Environment.getExternalStorageDirectory()。getAbsolutePath() )+ 「/影片/ chrono.mp4」); 意圖意圖=新意圖(Intent.ACTION_VIEW); intent.setDataAndType(VIDEOURI, 「應用程序/ x-mpegURL」); intent.putExtra(EXTRA_VIDEO_LIST,新Parcelable [] {videoUri}); intent.putExtra(EXTRA_POSITION,10000); startActivity(intent); } }); – dindinii