在另一个班级

时间:2016-01-29 07:49:36

标签: android

我正在尝试在另一个类中创建动态视频,并在MainActivity中调用该类方法。但这包含错误。

package com.gst_sdk_tutorials.tutorial_4;

    import java.io.File;
    import java.util.ArrayList;

    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.media.MediaPlayer;
    import android.widget.RelativeLayout;
    import android.widget.Toast;
    import android.widget.VideoView;

    public class VideoClass extends Activity
    {
        VideoView v;
        Context vContext;
        SQLiteDatabase SQLdb;
        String tmp;
        RelativeLayout rl;
        RelativeLayout.LayoutParams params;

        Cursor CurserTemplateVideo;

        String tmpV;
        int videoID;
        int videoWidth;
        int videoHeight;
        int videoX;
        int videoY;

        int videoCount;
        int videolistCount=2;
        ArrayList<String> videolist = new ArrayList<String>();

        public VideoClass(String tmp, SQLiteDatabase SQLdb, RelativeLayout rl, RelativeLayout.LayoutParams params,Context vContext) 
        {
            videolist.add("1.mp4");
            videolist.add("2.mp4");

            this.vContext = vContext;
            this.SQLdb = SQLdb;
            this.tmp = tmp;
            this.rl = rl;
            this.params = params;
            getVideoZoneList();
        }

        public void getVideoZoneList() 
        {
            try 
            {   
                CurserTemplateVideo = SQLdb.rawQuery("SELECT * FROM TemplateVideo where Template='"+tmp+"'", null);

                if(CurserTemplateVideo.getCount()==0)
                {
                    return;
                }

                while(CurserTemplateVideo.moveToNext())
                {
                    tmpV = CurserTemplateVideo.getString(0);
                    videoID = Integer.parseInt(CurserTemplateVideo.getString(1));
                    videoWidth = Integer.parseInt(CurserTemplateVideo.getString(2));
                    videoHeight = Integer.parseInt(CurserTemplateVideo.getString(3));
                    videoX = Integer.parseInt(CurserTemplateVideo.getString(4));
                    videoY = Integer.parseInt(CurserTemplateVideo.getString(5));

                    newVideoView(videoID,videoWidth,videoHeight,videoX,videoY);
                }
            } 
            finally 
            {
                if (CurserTemplateVideo!=null) 
                {
                    CurserTemplateVideo.close();
                }
            }
        }

        public void newVideoView(int count, int w, int h, int x, int y) 
        {
            v = new VideoView(vContext);
            v.setId(count);
            v.setX(x);
            v.setY(y);
            params = new RelativeLayout.LayoutParams(w,h);
            //params.setMargins(5, 5, 5, 5);
            rl.addView(v,params);

            playVideo();
        }

        public void playVideo() 
        {
            v.setVideoPath("/sdcard/Signage/videos/"+videolist.get(0));
            v.start();

            v.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
            {
                @Override
                public void onCompletion(MediaPlayer mp) 
                {
                    try 
                    {
                        File dir1 = new File("/sdcard/Signage/videos/");
                        final File file1[]=dir1.listFiles();
                        videoCount++;

                        if(file1.length==0)
                        { 
                            v.stopPlayback();
                        }
                        else
                        {
                            if(videoCount<videolistCount)
                             {
                                 String filePath = file1[videoCount].toString();
                                 File file = new File(filePath);
                                 long fileSize = file.length();


                                 if(fileSize == 0)
                                 {
                                     file.delete();
                                     videoCount++;
                                 }
                                 else if(videoCount >= videolistCount)
                                 {
                                     videoCount=0;
                                 }
                                 else
                                 {
                                    try 
                                    {
                                        v.setVideoPath("/sdcard/Signage/videos/"+videolist.get(videoCount));
                                        v.start();
                                    } 
                                    catch (Exception e) 
                                    {
                                        Toast.makeText(getBaseContext(),"Video Play Error!",Toast.LENGTH_LONG).show();
                                    }
                                 }
                             }
                             else
                             {
                                 videoCount=0;
                                 v.setVideoPath("/sdcard/Signage/videos/"+videolist.get(videoCount));
                                 v.start();
                             }
                        } 
                    } 
                    catch (Exception e) 
                    {
                        Toast.makeText(getBaseContext(),"Video Play Error!",Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }

公共类MainActivity扩展了Activity {

Context vContext;

RelativeLayout rl;
RelativeLayout.LayoutParams params;

SQLiteDatabase SQLdb;

String tmp = "3";

public CreateSQLDBClass DBClass = null;
public VideoClass VideoClass = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    decorView.setSystemUiVisibility(uiOptions);
    //decorView.setSystemUiVisibility(8);
    decorView.onWindowFocusChanged(true);

    setContentView(R.layout.activity_main);

    SQLdb = openOrCreateDatabase("Signage",Context.MODE_PRIVATE, null);

    rl = (RelativeLayout) findViewById(R.id.container);


    DBClass = new CreateSQLDBClass();
    VideoClass = new VideoClass(tmp,SQLdb,rl,params,vContext);

    if (savedInstanceState == null) 
    {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

听到错误消息......

01-01 07:26:06.060:D / AndroidRuntime(5687):关闭虚拟机 01-01 07:26:06.060:W / dalvikvm(5687):threadid = 1:线程退出未捕获异常(组= 0x41a43930) 01-01 07:26:06.070:E / AndroidRuntime(5687):致命异常:主要 01-01 07:26:06.070:E / AndroidRuntime(5687):java.lang.RuntimeException:无法启动活动ComponentInfo {com.gst_sdk_tutorials.tutorial_4 / com.gst_sdk_tutorials.tutorial_4.MainActivity}:java.lang.IndexOutOfBoundsException:无效索引0,大小为0 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.os.Handler.dispatchMessage(Handler.java:99) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.os.Looper.loop(Looper.java:137) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.ActivityThread.main(ActivityThread.java:5041) 01-01 07:26:06.070:E / AndroidRuntime(5687):at java.lang.reflect.Method.invokeNative(Native Method) 01-01 07:26:06.070:E / AndroidRuntime(5687):at java.lang.reflect.Method.invoke(Method.java:511) 01-01 07:26:06.070:E / AndroidRuntime(5687):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 01-01 07:26:06.070:E / AndroidRuntime(5687):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-01 07:26:06.070:E / AndroidRuntime(5687):at dalvik.system.NativeStart.main(Native Method) 01-01 07:26:06.070:E / AndroidRuntime(5687):引起:java.lang.IndexOutOfBoundsException:索引0无效,大小为0 01-01 07:26:06.070:E / AndroidRuntime(5687):at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 01-01 07:26:06.070:E / AndroidRuntime(5687):at java.util.ArrayList.get(ArrayList.java:304) 01-01 07:26:06.070:E / AndroidRuntime(5687):at com.gst_sdk_tutorials.tutorial_4.VideoClass.playVideo(VideoClass.java:94) 01-01 07:26:06.070:E / AndroidRuntime(5687):at com.gst_sdk_tutorials.tutorial_4.VideoClass.newVideoView(VideoClass.java:89) 01-01 07:26:06.070:E / AndroidRuntime(5687):at com.gst_sdk_tutorials.tutorial_4.VideoClass.getVideoZoneList(VideoClass.java:67) 01-01 07:26:06.070:E / AndroidRuntime(5687):at com.gst_sdk_tutorials.tutorial_4.VideoClass。(VideoClass.java:44) 01-01 07:26:06.070:E / AndroidRuntime(5687):at com.gst_sdk_tutorials.tutorial_4.MainActivity.onCreate(MainActivity.java:154) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.Activity.performCreate(Activity.java:5113) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-01 07:26:06.070:E / AndroidRuntime(5687):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-01 07:26:06.070:E / AndroidRuntime(5687):......还有11个 01-01 07:26:06.420:I / Process(5687):发送信号PID:5687 SIG:9

0 个答案:

没有答案