简体   繁体   中英

Android Studio: can´t resolve symbol R

I know this question has been asked several times, but no answers are useful due to my context. The thing is, I just opened my first project (in my actual computer) all the "R." in the mainActivity.java turn red. Do anyone know what are the causes for this problem and how it can be fixed?

Here is the .java main activity

package xyz.elblog.elblog;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.graphics.drawable.Drawable;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.view.View.OnClickListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import android.widget.FrameLayout;
import android.text.Html;
import android.app.Activity;
public class home extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        String readJSON = getJSON("http://blogging.mysite.com.mx/json.php");
        try{
            JSONObject jsonObject = new JSONObject(readJSON);
           String seo = jsonObject.getString("seo");
            WebView bprimary = (WebView) findViewById(R.id.bprimary);
            bprimary.loadData(seo,"text/html","UTF-8");
        } catch(Exception e){e.printStackTrace();}
        finally {
            System.out.println("Success");
        }

    }

    public String getJSON(String address){
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(address);
        try{
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if(statusCode == 200){
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while((line = reader.readLine()) != null){
                    builder.append(line);
                }
            } else {
                Log.e(home.class.toString(),"Failedet JSON object");
            }
        }catch(ClientProtocolException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return builder.toString();
    }
}

therefore this the home.xml

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

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/bprimary"></FrameLayout>

</LinearLayout>

im trying to get with json html and show on frame layout

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "xyz.elblog.elblog"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
}

The problem due to some error in your xml files. So, check all your xml layouts, styles, and selectors for the error. And also check the drawables file names. Like your add some pics in drawables having names containing symbols like '-' etc which is incorrect way to giving name. So, find the error and rebuild your project. This issues will be solved easily.

This error occurs when there is any error in res dir's files. So first try to fix those problems(in layout, drawables, styles, values etc etc). After fixing rebuild your project and the problem will be solved.. Happy Coding

  1. Check if there is no error in XML file, and cross-verify your recent modifications
  2. Try " Sync Project With Gradle Files "

This has worked for me

尝试在 Gradle 文件中编译 HttpClient 库,例如:-

compile files('libs/fluent-hc-4.5.jar') compile files('libs/httpclient-4.5.jar') compile files('libs/httpclient-cache-4.5.jar') compile files('libs/httpclient-win-4.5.jar') compile files('libs/httpcore-4.4.1.jar') compile files('libs/httpmime-4.5.jar') compile files('libs/jna-4.1.0.jar') compile files('libs/jna-platform-4.1.0.jar')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM