android - OnClickListener中的Android URI

标签 android youtube uri android-videoview onclicklistener

嗨,大家好,我对Android有问题...
这是我的代码的一部分:

setContentView(R.layout.activity_main);
TableLayout MainTable = (TableLayout) findViewById(R.id.main_table);
JSONArray Jobj = new JSONArray(result_set);
String url_img = null;
String url_video = null; //MUST CONTAINS THE URL OF MY VIDEO

for (int i = 0; i < Jobj.length(); i++) {
    TableRow row = new TableRow(getApplicationContext());
    row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    row.setPadding(0, 14, 2, 14);

    JSONObject titoli = Jobj.getJSONObject(i);
    Integer id = titoli.getInt("id_video");
    String titolo = titoli.getString("nome_video");
    String immagini = titoli.getString("video_img");
    String video_url = titoli.getString("url_video"); 
    /* video_url : this is the variable that i change (and contains the relative path that i obtains parsing json array) */

    Pattern p = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
    Matcher m = p.matcher(immagini);

    Pattern v = Pattern.compile("^/video/[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
    Matcher vm = v.matcher(video_url);

    // Path video
    if (vm.matches() == false) {
        url_video = path_youtube + video_url; // SECOND ERROR HERE
    } else if (vm.matches() == true) {
        url_video = path_an_tv + video_url; // SECOND ERROR HERE
    }

    // Path image
    if (m.matches() == false) {
        url_img = path + immagini;
    } else if (m.matches() == true) {
        url_img = immagini;
    }

    ImageView img = new ImageView(getApplicationContext());
    img.setAdjustViewBounds(true);
    img.setMaxHeight(140);
    img.setMaxWidth(140);
    Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url_img).getContent());
    img.setImageBitmap(bitmap);

    LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    final TextView txt = new TextView(getApplicationContext());
    txt.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
    txt.setLayoutParams(params);
    txt.setTextSize(18);
    txt.setBackgroundColor(Color.WHITE);
    txt.setTypeface(null, Typeface.BOLD);
    txt.setTextColor(Color.BLACK);
    txt.setId(id);
    txt.setText(titolo);
    txt.setClickable(true);

    row.addView(img);
    row.addView(txt);
    MainTable.addView(row);

    txt.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_video))); 
                   /* FIRST ERROR HERE, url_video contains the absolute url of my video*/
        }
    });
}

当我尝试编译我的项目时,发生错误:

*无法在以其他方法定义的innerner类内引用非最终变量url_video ... *

如果我将网址视频更改为final,则会发生另一个错误:

无法分配最终的局部变量。它必须为空白并且不使用复合赋值

我该如何解决?

更新,我将它替换为:
final TextView txt = new TextView(getApplicationContext()); 
     txt.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL );
     txt.setLayoutParams(params); txt.setTextSize(18); txt.setBackgroundColor(Color.WHITE);
     txt.setTypeface(null, Typeface.BOLD); txt.setTextColor(Color.BLACK); 
     txt.setId(id); txt.setTag(url_video); txt.setText(titolo); txt.setClickable(true); 

     row.addView(img);
     row.addView(txt);  
     MainTable.addView(row);

     txt.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
       startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse((String) txt.getTag())));
      }
     });
    }

最佳答案

在类级别private String video_url;中定义它

您无法访问onCreate()OnClick()中定义的变量,因为单击时oncreate的范围已结束。因此,您必须保持变量作用域有效。

更新

是的,很简单,

public class YourClass {

    private String video_url;

    public void OnCreate()
    {
    }
}

关于android - OnClickListener中的Android URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21112889/

相关文章:

android - gridview底部的ProgressBar

perl - 有没有办法检查参数是否用单引号引起来?

web-services - REST URI 设计 : how update

javascript - 在带有 window.open 的 Firefox 上,重音在 ISO-8859-1 中错误显示

android - 如何在 Android 中创建颜色选择器(类似于颜色状态列表,但用于自定义属性)?

android - ListView 中的 "Separator"

android - getPlaceByID() 总是失败并出现 java.lang.IllegalArgumentException : Buffer is closed

video - 如何在 Joomla 1.5.23 中嵌入 YouTube 视频?

javascript - 强制 Youtube 嵌入以高清播放(2016 版)

wordpress - 在Shockwave Player中显示的YouTube视频,而不是YouTube视频