OnClickListener中的Android URI

时间:2014-01-14 11:51:25

标签: 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*/
        }
    });
}

当我尝试编译项目时,会出现错误:

*不能在不同方法中定义的内部类中引用非最终变量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())));
      }
     });
    }

2 个答案:

答案 0 :(得分:0)

在班级private String video_url;

中定义它

您无法访问onCreate()OnClick()内定义的变量,因为当您获得点击时,oncreate的范围已经结束。所以你必须让变量范围保持活着。

<强>更新

是的,很简单,

public class YourClass {

    private String video_url;

    public void OnCreate()
    {
    }
}

答案 1 :(得分:0)

只需将其设为实例变量

即可
String url_video = null;

这就是定义这种不活动。