【问题标题】:Android URI in OnClickListenerOnClickListener 中的 Android URI
【发布时间】:2014-01-14 11:51:24
【问题描述】:

嗨,伙计,我遇到了 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...*

如果我将 url 视频更改为 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())));
      }
     });
    }

【问题讨论】:

    标签: android youtube uri android-videoview onclicklistener


    【解决方案1】:

    在类级别定义它private String video_url;

    您无法在OnClick() 中访问在onCreate() 中定义的变量,因为当您获得点击时,oncreate 的范围就结束了。所以你必须保持变量作用域有效。

    更新

    是的,很简单,

    public class YourClass {
    
        private String video_url;
    
        public void OnCreate()
        {
        }
    }
    

    【讨论】:

    • 不要忘记删除您的字符串 video_url;在 oncreate 中
    • Arju,你的问题不成立!我动态地生成布局,使用此功能我想显示视频列表,当按下视频标题时,功能“onclicklistener”必须启动 1 个新活动,打开 youtube,使用正确的链接(url_video)显示我的视频。 ..
    【解决方案2】:

    只需将其设为实例变量

    String url_video = null;
    

    这就是定义这种不活动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多