0

OK - so I typically develop for iOS but have a client who needs a hand in a pinch.

I followed a tutorial, which was very straightforward and helpful. I created everything as it had asked me to (or maybe I didn't - which is why I'm here).

Any help would be greatly appreciated. We are really in a bind here!

You're welcome to come in over join.me as well and take a look. I think it could be something incredibly simple that I am too ignorant/inexperienced with this platform to see.

http://www.tutorialspoint.com/android/android_webview_layout.htm

The issue could be very simple. On these lines, I am getting errors which Eclipse will not override:

  setContentView(R.layout.activity_main);
  field = (EditText)findViewById(R.id.urlField);
  browser = (WebView)findViewById(R.id.webView1);

Error is "R cannot be resolved to a variable

Here is my MainActivity.java

package com.example.WebView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

   private EditText field;
   private WebView browser;

   @Override        
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      field = (EditText)findViewById(R.id.urlField);
      browser = (WebView)findViewById(R.id.webView1);
      browser.setWebViewClient(new MyBrowser());
   }


   public void open(View view){
      String url = field.getText().toString();
      browser.getSettings().setLoadsImagesAutomatically(true);
      browser.getSettings().setJavaScriptEnabled(true);
      browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
      browser.loadUrl("http://www.magiktab.com/app");

   }
   private class MyBrowser extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl("http://www.magiktab.com/app");
         return true;
      }
   }


   }

Here is my activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hello_world" />

   <EditText
      android:id="@+id/urlField"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignTop="@+id/textView1"
      android:layout_centerHorizontal="true"
      android:ems="10" />

   <Button
      android:id="@+id/button1" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/urlField"
      android:layout_centerHorizontal="true"
      android:onClick="open"
      android:text="@string/browse" />

   <WebView
      android:id="@+id/webView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView1"
      android:layout_alignParentBottom="true"
      android:layout_below="@+id/button1" />


</RelativeLayout>

And of course, my manifest

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

   <uses-sdk
  android:minSdkVersion="8"
  android:targetSdkVersion="17" />
   <uses-permission android:name="android.permission.INTERNET"/>

   <application
      android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
     android:name="com.example.webview.MainActivity"
     android:label="@string/app_name" >
     <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>

  • I don't see anywhere where R is defined myself... can you point me to where you believe you are defining it? – explodingcreeperssss May 11 '14 at 21:18
  • 2
    closing `` tag in Android manifest seems to be missing – donfuxx May 11 '14 at 21:18
  • R.java is autogenerated in android. @explodingcreeperssss – donfuxx May 11 '14 at 21:19
  • @donfuxx the tag is there. guess this stripped it out - thanks for pointing that out though. – Tony LaManna May 11 '14 at 21:21
  • @explodingcreeperssss great question - i don't actually know where TO define it. Maybe that would be a push in the correct direction? – Tony LaManna May 11 '14 at 21:21
  • 1
    There is a problem in one of your resources or with your manifest, which is preventing `R.java` from being generated for you. Your IDE should be pointing out these problems. For example, in Eclipse, there will be a red X over the icon for the file in the Package Explorer, or there will be messages in the Errors view. – CommonsWare May 11 '14 at 21:22
  • @donfuxx do you have to include anything? (or something similar, to tell android what R is, even though it IS auto generated) – explodingcreeperssss May 11 '14 at 21:22
  • 1
    "R cannot be resolved to a variable" is a pretty common error that every android programmer will experience. See also [related question](http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error) – donfuxx May 11 '14 at 21:23
  • @CommonsWare OK - got it. I do see them, but the red x's are giving only the information (and I am using Eclipse), that simply says "R cannot be resolved to a variable" – Tony LaManna May 11 '14 at 21:25
  • The message "R cannot be resolved to a variable" will come from errors in Java code. Resources are not Java code. They are files in your `res/` directory. The manifest is not Java code. That is the `AndroidManifest.xml` file that is in your project's root directory. As I wrote, there is a problem in one of your resources or with your manifest, which is preventing `R.java` from being generated for you. Your IDE should be pointing out these problems. For example, in Eclipse, there will be a red X over the icon for the file in the Package Explorer, or there will be messages in the Errors view. – CommonsWare May 11 '14 at 21:36
  • @CommonsWare you were mostly correct. I also found an issue with the permissions as donfuxx pointed out. I am now able to run the app. Just having some issues 1) having eclipse find my device and 2) the app actually loading the default URL specified (which it shows up blank) – Tony LaManna May 11 '14 at 21:51

1 Answers1

0

This may happen when your application package name is different from the activity package name;

import com.your.app.package.name.R;

usually helps.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127