Загрузить URL из ImageView - PullRequest
       18

Загрузить URL из ImageView

0 голосов
/ 25 апреля 2019

Мне нужно загрузить URL-адрес внутри моего приложения одним нажатием кнопки ImageView.Я получаю приложение для загрузки, пока не начну пытаться назначить намерения (в MainActivity.java) для моих ImageViews (в activity_main.xml).

Я пробовал ImageButton vs ImageView;и у обоих есть свои проблемы, на которые я не могу найти ответы.Я, наконец, разочаровался в imageButton и теперь пытаюсь везти в ImageView.Все компоненты Android обновлены, включая само программное обеспечение, Gradle и плагины.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/mainBackground"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/s"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:clickable="true"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="16dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/y"
        app:layout_constraintEnd_toStartOf="@+id/v"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/s"
        tools:visibility="visible" />

    <ImageView
        android:id="@+id/v"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:clickable="true"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/q"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/s"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/v"
        tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.app.sega;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.net.URI;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView img_s = (ImageView) findViewById(R.id.s);
        img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse("http://southeastgeorgiatoday.com"));
                startActivity(intent);
            }
        });

        ImageView img_v = (ImageView) findViewById(R.id.q);
        img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse("http://us7.maindigitalstream.com/2780/"));
                startActivity(intent);
            }
        });

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.sega" >
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <data android:scheme="http" android:host="www.southeastgeorgiatoday.com" />
                <data android:scheme="http" android:host="us7.maindigitalstream.com/2780/" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Цель - открыть URL-адреснамерение внутри приложения с помощью щелчка ссылочной ImageView.Но, поскольку этот намеренный скрипт включен в MainActivity.java, он не будет собираться, не говоря уже об установке и запуске.Я пробовал его как на виртуальном, так и на реальном устройстве через эмулятор Android, и он не будет установлен ни на одном из них.

Вот сообщение об ошибке, которое я получаю от LogCat во время процесса сборки ...

C:\Users\webwi\Documents\_MobileApps\SEGA\app\src\main\java\com\app\sega\MainActivity.java:23: error: cannot find symbol
        img.setOnClickListener(new View.OnClickListener() {
        ^
  symbol:   variable img
  location: class MainActivity
C:\Users\webwi\Documents\_MobileApps\SEGA\app\src\main\java\com\app\sega\MainActivity.java:33: error: cannot find symbol
        img.setOnClickListener(new View.OnClickListener() {
        ^
  symbol:   variable img
  location: class MainActivity
2 errors

Кроме того, возможно, стоит упомянуть в обеих строках намерения, которые начинаются с " img .setOnClickListener ...", "img" перед "."красный.

Очевидно, что что-то не так с моими намерениями, я просто не уверен, что?Любой совет.

...