gpt4 book ai didi

java - 为什么我无法定义数组?

转载 作者:行者123 更新时间:2023-12-02 11:12:43 24 4
gpt4 key购买 nike

我无法在android studio mainActivityFragment.java中定义数组;它说这是不可达的声明。

我的代码是:

package com.example.android.sunshine;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);



String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/40",
"Tuesday - Cloudy - 72/63",
"Wednesday - Asteroids - 75/65",
"Thursday - Heavy Rain - 65/56",
"Friday - Help Trapped in weather Station - 60/51",
"Saturday - Sunny - 80/68"};
}
List<String> weekForecast = new ArrayList<String>(
Arrays.asList(forecastArray)
);
}

最佳答案

错误在这里

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/40",
"Tuesday - Cloudy - 72/63",
"Wednesday - Asteroids - 75/65",
"Thursday - Heavy Rain - 65/56",
"Friday - Help Trapped in weather Station - 60/51",
"Saturday - Sunny - 80/68"};
}

您应该将 array定义放在 return语句之前
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray = { "Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/40", "Tuesday - Cloudy - 72/63",
"Wednesday - Asteroids - 75/65",
"Thursday - Heavy Rain - 65/56",
"Friday - Help Trapped in weather Station - 60/51",
"Saturday - Sunny - 80/68" };
return inflater.inflate(R.layout.fragment_main, container, false);
}

希望能有所帮助

关于java - 为什么我无法定义数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com