-3

我的代码做错了吗?出于某种原因,我无法从我的文本视图中获取activity_main.xml与我的MainActivity.java文件链接。

这是我的代码

    package com.dredaydesigns.radiostationfinder;

import android.R;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.Bind;


public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {
String longitude;
    String latitude;
   // String HTTPRadioURL = "https://transition.fcc.gov/fcc-bin/fmq?state=&call=&city=&arn=&serv=FC&vac=3&freq=0.0&fre2=107.9&facid=&asrn=&class=&dkt=&list=0&dist=100&dlat2="
        //    + latitude + "&mlat2=&slat2=&NS=N&dlon2="
          //  + longitude +"&mlon2=&slon2=&EW=W&size=9";

    public static final String TAG = MainActivity.class.getSimpleName();
    private RadioData mRadioData;

    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;


private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
@Bind(R.id.longitudeLabel) TextView mLongitudeLabel;
@Bind(R.id.latitudeLabel) TextView mLatitudeLabel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_content);
    ButterKnife.bind(this);

        double latitude = 32;
        double longitude = -96;
        double latitudeStations;
        double longitudeStations;
        @Bind(R.id.latitudeLabel) TextView latitude;
        @Bind(R.id.longitudeLabel) TextView longitude;

        final RadioData[] mRadioData = new RadioData[1];
       //String radioFinderURL = "http://data.fcc.gov/lpfmapi/rest/v1/lat/" + latitude + "/long/" + longitude + "?format=json&secondchannel=true";
       //String HTTPRadioURL = "https://transition.fcc.gov/fcc-bin/fmq?state=&call=&city=&arn=&serv=FC&vac=3&freq=0.0&fre2=107.9&facid=&asrn=&class=&dkt=&list=0&dist=100&dlat2="
          //      + latitude + "&mlat2=&slat2=&NS=N&dlon2="
           //     + longitude +"&mlon2=&slon2=&EW=W&size=9";
        String radioFinderURL = "http://dredaycreative.com/json/radioData.json";

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(radioFinderURL)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG,jsonData);
                    if (response.isSuccessful()) {
                        mRadioData[0] = getCurrentDetails(jsonData);

                    }

                } catch (IOException e) {
                    Log.e(TAG, "Exception Caught: ", e);
                }
                catch(JSONException e){
                    Log.e(TAG, "Exception Caught:", e);
                }
            }
        });








    }

    private RadioData getCurrentDetails(String jsonData) throws JSONException {
        JSONObject radioData = new JSONObject(jsonData);
        String callSign;
        double latitudeStations;
        double longitudeStations;
        int frequency;
        JSONArray jsonArray = radioData.getJSONArray("array");
        for(int i =0; i<jsonArray.length(); i++){
            callSign = radioData.getJSONObject(i+"")
                    .getJSONArray("array").getJSONObject(i).getString("FIELD1");
            frequency = Integer.parseInt(radioData.getJSONObject(i+"")
                    .getJSONArray("array").getJSONObject(i).getString("FIELD2"));
            longitudeStations = Double.parseDouble(radioData.getJSONObject(i+"")
                    .getJSONArray("array").getJSONObject(i).getString("FIELD20"));
            latitudeStations = Double.parseDouble(radioData.getJSONObject(i+"")
                    .getJSONArray("array").getJSONObject(i).getString("FIELD24"));
        Log.i(TAG, "From JSON: " + callSign + frequency + latitudeStations  + longitudeStations);

        RadioData radioFinder = new RadioData();

        }
            return new RadioData();



    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
                .addApi(LocationServices.API)
                .build();


    }


    @Override
    public void onConnected(Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            latitudeLabel.setText(String.valueOf(mLastLocation.getLatitude()));
            longitude.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }


    @Override
    public void onConnectionSuspended(int i) {

    }


}

这是我的 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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity"
                android:background="#ffffed">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Find Stations!"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#010101"/>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_above="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/latitudeLabel"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button"
        android:layout_alignEnd="@+id/button"
        android:layout_marginTop="49dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/longitudeLabel"
        android:layout_below="@+id/latitudeLabel"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button"
        android:layout_marginTop="72dp"/>

</RelativeLayout>

我之前什至没有使用过它,Butterknife但我想也许我在我的依赖项中激活了它,所以你必须使用它。但是没有它并尝试旧方法,我仍然无法让它工作,如下所示。

double latitude = 32;
double longitude = -96;
double latitudeStations;
double longitudeStations;
latitude = (TextView) findViewById(R.id.latitudeLabel);
longitude = (TextView) findViewById(R.id.longitudeLabel);

它总是出现red,如果我尝试运行它,那么它会得到一个无法解析的符号。

按照这里的要求是gradle文件!谢谢!

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.dredaydesigns.radiostationfinder"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.googlecode.json-simple:json-simple:1.1'
    compile 'com.google.code.gson:gson:1.7.2'

}
4

1 回答 1

2

你忘了调用ButterKnife.bind(this)你的onCreate方法。如果你不这样做,butterknife 不会注入你的观点,你就会遇到问题。

于 2015-08-16T02:53:19.893 回答