[android]错误:(100, 54) 错误︰ 非法开始的表达式

标签: Java Android windows
发布时间: 2016/8/28 2:50:25
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这是文件的代码 MainActivity.java

public class MainActivity extends AppCompatActivity {
    ListView lstVideo;
    ArrayList<Bitmap> arrHinh= new ArrayList<Bitmap>();
    ArrayList<Video> arrVideo=new ArrayList<Video>();
    String playlistId="LL-5bWlJvcvfR...........";
    final String API_KEY="AIza............";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lstVideo=(ListView)findViewById(R.id.lstVideo);
        new ParseYoutube().execute();
    }
    private class ParseYoutube extends AsyncTask<Void, Void, Void> {
        String time, urlHinh, title, idVideo;

        @Override
        protected Void doInBackground(Void... params) {
            if (getJsonFromUrl() != null) {
                try {
                    JSONObject jsonObbject = new JSONObject(getJsonFromUrl());
                    JSONArray jsonItems = jsonObbject.getJSONArray("items");
                    if (jsonItems.length() > 0) {
                        for (int i = 0; i < jsonItems.length(); i++) {
                            JSONObject jsonItem = jsonItems.getJSONObject(i);
                            JSONObject jsonsnippet = jsonItem.getJSONObject("snippet");
                            title = jsonsnippet.getString("title");
                            time = jsonsnippet.getString("publishedAt");
                            JSONObject jsonThum = jsonsnippet.getJSONObject("thumbnails");
                            JSONObject jsondefault = jsonThum.getJSONObject("default");
                            urlHinh = jsondefault.getString("url");
                            JSONObject jsonResource = jsonsnippet.getJSONObject("resourceId");
                            idVideo = jsonResource.getString("videoId");

                            Video video = new Video();
                            video.setTime(time);
                            video.setTitle(title);
                            video.setUrlID(idVideo);
                            arrVideo.add(video);

                            URL url = new URL(urlHinh);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            InputStream inputStream = httpURLConnection.getInputStream();
                            Bitmap bm = BitmapFactory.decodeStream(inputStream);
                            arrHinh.add(bm);

                        }
                    }
                } catch (JSONException | MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            super.onPostExecute(aVoid);
            lstVideo.setAdapter(new YoutubeAdapter(MainActivity.this, arrVideo, arrHinh));
        }
    }


    public String getJsonFromUrl() {
        URL url;
        try {
            url = new URL("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" + playlistId + "&key=" + API_KEY);
            URLConnection urlConnection = url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder builder = new StringBuilder();
            String line = "";
            while (line = bufferedReader.readLine()!== null)
            {
                builder.append(line + "\n");
            }
            inputStream.close();
            return builder.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是错误消息选项卡中︰

错误:(100, 54) 错误︰ 非法开始的表达式

错误︰ 执行失败的任务 ':app:compileDebugJavaWithJavac'。

编译失败;请参阅编译器错误输出的详细信息。

我不知道为什么我不能 debug 这款应用程序

解决方法 1:

你给了 extra = 签到线 77.改变它。放回原处。现在,代码将

public class MainActivity extends AppCompatActivity {
    ListView lstVideo;
    ArrayList<Bitmap> arrHinh= new ArrayList<Bitmap>();
    ArrayList<Video> arrVideo=new ArrayList<Video>();
    String playlistId="LL-5bWlJvcvfR...........";
    final String API_KEY="AIza............";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lstVideo=(ListView)findViewById(R.id.lstVideo);
        new ParseYoutube().execute();
    }
    private class ParseYoutube extends AsyncTask<Void, Void, Void> {
        String time, urlHinh, title, idVideo;

        @Override
        protected Void doInBackground(Void... params) {
            if (getJsonFromUrl() != null) {
                try {
                    JSONObject jsonObbject = new JSONObject(getJsonFromUrl());
                    JSONArray jsonItems = jsonObbject.getJSONArray("items");
                    if (jsonItems.length() > 0) {
                        for (int i = 0; i < jsonItems.length(); i++) {
                            JSONObject jsonItem = jsonItems.getJSONObject(i);
                            JSONObject jsonsnippet = jsonItem.getJSONObject("snippet");
                            title = jsonsnippet.getString("title");
                            time = jsonsnippet.getString("publishedAt");
                            JSONObject jsonThum = jsonsnippet.getJSONObject("thumbnails");
                            JSONObject jsondefault = jsonThum.getJSONObject("default");
                            urlHinh = jsondefault.getString("url");
                            JSONObject jsonResource = jsonsnippet.getJSONObject("resourceId");
                            idVideo = jsonResource.getString("videoId");

                            Video video = new Video();
                            video.setTime(time);
                            video.setTitle(title);
                            video.setUrlID(idVideo);
                            arrVideo.add(video);

                            URL url = new URL(urlHinh);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            InputStream inputStream = httpURLConnection.getInputStream();
                            Bitmap bm = BitmapFactory.decodeStream(inputStream);
                            arrHinh.add(bm);

                        }
                    }
                } catch (JSONException | MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            super.onPostExecute(aVoid);
            lstVideo.setAdapter(new YoutubeAdapter(MainActivity.this, arrVideo, arrHinh));
        }
    }


    public String getJsonFromUrl() {
        URL url;
        try {
            url = new URL("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" + playlistId + "&key=" + API_KEY);
            URLConnection urlConnection = url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder builder = new StringBuilder();
            String line = "";
            while (line = bufferedReader.readLine()!= null) // while (line = bufferedReader.readLine()!== null) you need to remove extra = sign
            {
                builder.append(line + "\n");
            }
            inputStream.close();
            return builder.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

同意childofsoong

赞助商