我是android studio的新手,我正在尝试运行此代码。
我的代码:
package app.sunshine.android.example.com.sunshine;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
// Create some dummy data for the ListView. Here's a sample weekly
// Represented as "day, whether, high/low"
String[] forecastArray= {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/40",
"Weds - Cloudy - 72/63",
"Thurs - Asteroids - 75/65",
"Fri - Heavy Rain - 65/56",
"Sat - HELP TRAPPED IN WEATHERSTATION - 60/51",
"Sun - Sunny - 80/68"
};
List<String> weekForecast = new ArrayList<String>(
Arrays.asList(forecastArray));
// Now that we have some dummy forecast data, create an ArrayAdapter.
// The ArrayAdapter will take data from a source ( Like our dummy forecast
// use to populate the ListView it's attached to
mforecastAdapter =
new ArrayAdapter<String>(
//The current context( this fragment's parent activity
getActivity(),
// ID of list item layout
R.layout.list_item_forecast,
// ID of the textview to populate
R.id.list_item_forecast_textview,
// Forecast data
weekForecast);
// Get a reference to the LIstView and attach this adpater to it
ListView listView = (ListView) rootView.findViewById(
R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
return rootView;
}
}
}
但是我收到以下错误:cannot find symbol variable
我尝试清理项目gradle并重新启动android应用程序。 但我仍然遇到这个问题
任何帮助将不胜感激:)
我正在关注本教程https://www.udacity.com/course/viewer#!/c-ud853/l-1395568821/e-1395668601/m-1395668603
答案 0 :(得分:3)
您需要声明mForecastAdapter。
public static class PlaceholderFragment extends Fragment {
ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment() {
}
由于您还没有这样做,它无法识别变量mForecastAdapter。希望这会有所帮助。