Грешка:(17, 41) грешка: несъвместими типове: Изгледът не може да се преобразува в WebView

Опитвах се да създам бутон, който препраща към уеб страница и открих тази грешка (Грешка:(17, 41) грешка: несъвместими типове: Изгледът не може да бъде преобразуван в WebView). Опитах повечето от дадените решения, но нищо не работи. Кодировките са както следва.

SettingPage.java

package com.example.app.settings.main_class;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

import com.example.app.main_activity_class.MainActivity;
import com.example.app.settings.difficulty.DifficultyMain;
import com.example.app.settings.lock_sytem.LockSettings;
import com.example.app.settings.points_system.PointsSystemMain;
import com.example.app.R;
import com.example.app.settings.relaunch_system.RelaunchSettings;



public class SettingMain extends Activity {

//Main page for settings
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting_main_layout);
    Button lock = (Button) findViewById(R.id.lock);

    lock.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(SettingMain.this,     LockSettings.class);
            startActivity(i);
        }
    });


    Button relaunch = (Button) findViewById(R.id.relaunch);

    relaunch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(SettingMain.this, RelaunchSettings.class);
            startActivity(i);
        }
    });


    Button points = (Button) findViewById(R.id.points);

    points.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent ii = new Intent(SettingMain.this, PointsSystemMain.class);
            startActivity(ii);
        }
    });

    Button difficulty = (Button) findViewById(R.id.difficulty);

    difficulty.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent ai = new Intent(SettingMain.this,      DifficultyMain.class);
            startActivity(ai);
        }
    });

    Button webview = (Button) findViewById(R.id.webview1);

    webview.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent ai = new Intent(SettingMain.this, WebView.class);
            startActivity(ai);
        }
    });

    Button saveexit = (Button) findViewById(R.id.saveexit);

    saveexit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
}


@Override
protected void onPause() {
    super.onPause();
}
}

WebView.java

package com.example.app.settings.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;


import com.example.app.R;

public class WebView extends Activity {

private WebView webview;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    webview = (WebView) findViewById(R.id.webview1);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.google.com/");

}

private void loadUrl(String s) {
}

private WebSettings getSettings() {
    return null;
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="16" />

<uses-permission   android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.screen.portrait" />

<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/dtsfinal"
    android:label="@string/app_name"
    android:logo="@drawable/dothisfinal"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".main_activity_class.SplashScreen"
        android:theme="@style/AppBaseTheme3" >
        <intent-filter android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".main_activity_class.MainActivity"
        android:label=""
        android:screenOrientation="portrait" />
    <activity
        android:name=".camera.CameraActivity"
        android:screenOrientation="portrait" />
    <activity android:name=".drawing.Colourchoose" />
    <activity
        android:name=".settings.main_class.SettingMain"
        android:label="@string/app_name" />
    <activity android:name=".settings.lock_sytem.LockSettings" />
    <activity android:name=".settings.relaunch_system.RelaunchSettings"   />
    <activity android:name=".settings.points_system.PointsSystemMain" />
    <activity android:name=".statistics.StatisticsView" />
    <activity android:name=".drawing.BrushSize" />
    <activity android:name=".admin.AdminMainPage" />
    <activity android:name=".runtime.Runtime_Main" />
    <activity android:name=".runtime.ViewAll" />
    <activity android:name=".runtime.ViewByDate" />
    <activity android:name=".settings.difficulty.DifficultyMain" />
    <activity
        android:name=".awards.ClockAward"
        android:theme="@style/AppBaseTheme2" />
    <activity
        android:name=".awards.CoinAward"
        android:theme="@style/AppBaseTheme2" />
    <activity
        android:name=".awards.TrophyAward"
        android:theme="@style/AppBaseTheme2" />

    <receiver
        android:name=".settings.lock_sytem.MyAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@layout/admin" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>
    <receiver     android:name=".settings.relaunch_system.OnRelaunchReceive" />
    <receiver android:name=".settings.lock_sytem.OnLockReceive" />

    <activity
        android:name=".settings.webview.WebView"
        android:label="@string/title_activity_web_view" >
    </activity>
</application>

</manifest>

setting_main_layout.xml

<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ln1"
    android:gravity="center">


    <Button
        android:id="@+id/lock"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:text="Lock Settings" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ln2"
    android:gravity="center"
    android:layout_below="@+id/ln1">



    <Button
        android:id="@+id/relaunch"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Relaunching app settings" />
</LinearLayout>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Points"
    android:layout_below="@+id/ln2"
    android:id="@+id/points" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Select Difficulty"
    android:layout_below="@+id/points"
    android:id="@+id/difficulty" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Visit DoThis Website"
    android:layout_below="@+id/difficulty"
    android:id="@+id/webview1" />


<Button
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:textColor="#ff00f4ff"
    android:background="#ff2f2f2f"
    android:layout_alignParentBottom="true"
    android:text="Save and Exit"
    android:id="@+id/saveexit" />


</RelativeLayout>

activity_web_view.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

и съобщението за изграждане на gradle е както следва:

Информация: Задачи на Gradle [:app:assembleDebug] :app:preBuild :app:compileDebugNdk :app:preDebugBuild :app:checkDebugManifest :app:preReleaseBuild :app:prepareComAndroidSupportAppcompatV71800Library АКТУАЛЕН :app:prepareDebugDependencies UPTO-Aidle :Deapp:com -ДАТА :app:compileDebugRenderscript АКТУАЛЕН :app:generateDebugBuildConfig АКТУАЛЕН :app:generateDebugAssets АКТУАЛЕН ::mergeDebugAssets АКТУАЛЕН :app:generateDebugResValues ​​АКТУАЛЕН :app:generateDebugResources АКТУАЛНО :app:mergeDebugResources АКТУАЛНО :app:processDebugManifest АКТУАЛНО :app:processDebugResources АКТУАЛНО :app:generateDebugSources АКТУАЛНО :app:compileDebugJava /Users/mcm/ AndroidStudioProjects/MyApp/app/src/main/java/com/example/app/settings/webview/WebView.java Грешка:(18, 41) грешка: несъвместими типове: Изгледът не може да се преобразува в WebView Забележка: Някои входни файлове използват или замени остарял API. Забележка: Прекомпилирайте с -Xlint:deprecation за подробности. Грешка: Неуспешно изпълнение за задача „:app:compileDebugJava“. Компилацията е неуспешна; вижте изхода за грешка на компилатора за подробности. Информация: BUILD FAILED Информация: Общо време: 2,194 секунди Информация: 2 грешки Информация: 0 предупреждения Информация: Вижте пълния изход в конзолата

може ли някой да ми помогне с някои решения? Благодаря ви.


person marian    schedule 14.09.2015    source източник


Отговори (1)


Тук:

import android.webkit.WebView;

В SettingMain клас в момента импортира WebView клас от Android клас вместо ваш собствен, който е създаден от вас.

Премахнете импортирането на import android.webkit.WebView; и добавете следното импортиране във файл SettingMain.java:

import com.example.app.settings.webview.WebView;
person ρяσѕρєя K    schedule 14.09.2015
comment
промених го, но все още показва грешка при: webview = (WebView) findViewById(R.id.webview1); маркира се в червено с неконвертируем тип съобщение; не може да прехвърли android.view.View' към com.example.app.settings.webview.WebView - person marian; 14.09.2015
comment
@marian: къде се получава грешка? коя грешка? моля, покажете актуализиран код и се уверете, че сте почистили, възстановили и деинсталирали предишното приложение, преди да стартирате ново - person ρяσѕρєя K; 14.09.2015
comment
да, почистих, но грешката е до там. грешка при: webview = (WebView) findViewById(R.id.webview1); маркира се в червено с неконвертируем тип съобщение; не може да прехвърли android.view.View' към com.example.app.settings.webview.WebView - person marian; 14.09.2015
comment
актуализираният код: import android.app.Activity; импортиране на android.content.Intent; импортиране на android.os.Bundle; импортиране на android.view.View; импортиране на android.view.View.OnClickListener; импортиране на android.widget.Button; import com.example.app.main_activity_class.MainActivity; import com.example.app.settings.difficulty.DifficultyMain; import com.example.app.settings.lock_sytem.LockSettings; import com.example.app.settings.points_system.PointsSystemMain; импортиране com.example.app.R; import com.example.app.settings.relaunch_system.RelaunchSettings; import com.example.app.settings.webview.WebView; - person marian; 14.09.2015
comment
@marian: добавете import android.webkit.WebView; ред в WebView клас - person ρяσѕρєя K; 14.09.2015
comment
@marian: или променете private WebView webview; на private android.webkit.WebView webview; и webview = (WebView) findViewById(R.id.webview1); на webview = (android.webkit.WebView) findViewById(R.id.webview1); - person ρяσѕρєя K; 14.09.2015
comment
може да е проблемът с ids, Button and WebView ids са декларирани еднакви в xml файла, т.е. webView1, може ли това да е проблем? това е само предположение.. - person Satyen Udeshi; 14.09.2015
comment
@ρяσѕρєяK благодаря ти много,,,решението проработи :) - person marian; 14.09.2015