错误'无法解析符号'容器'在MainActivity中

时间:2016-04-08 19:58:19

标签: java android containers

下面是我的MainActivity.Java文件代码,我正面临一个错误,其中包含' Container'标记为Can't resolve Symbol 'container'。谁能帮助我,我在这里做错了什么?我的程序中根本没有任何其他错误。

package com.example.android.fragmentactivity;

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

 /**
  * ATTENTION: This was auto-generated to implement the App Indexing API.
  * See https://g.co/AppIndexing/AndroidStudio for more information.
  */
 private GoogleApiClient client;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction()
    .add(R.id.container, new PlaceholderFragment())
    .commit();
  }
  // ATTENTION: This was auto-generated to implement the App Indexing API.
  // See https://g.co/AppIndexing/AndroidStudio for more information.
  client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.menu_main, 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();

  //noinspection SimplifiableIfStatement
  if (id == R.id.action_settings) {
   return true;
  }

  return super.onOptionsItemSelected(item);

 }

 @Override
 public void onStart() {
  super.onStart();

  // ATTENTION: This was auto-generated to implement the App Indexing API.
  // See https://g.co/AppIndexing/AndroidStudio for more information.
  client.connect();
  Action viewAction = Action.newAction(
   Action.TYPE_VIEW, // TODO: choose an action type.
   "Main Page", // TODO: Define a title for the content shown.
   // TODO: If you have web page content that matches this app activity's content,
   // make sure this auto-generated web page URL is correct.
   // Otherwise, set the URL to null.
   Uri.parse("http://host/path"),
   // TODO: Make sure this auto-generated app deep link URI is correct.
   Uri.parse("android-app://com.example.android.fragmentactivity/http/host/path")
  );
  AppIndex.AppIndexApi.start(client, viewAction);
 }

 @Override
 public void onStop() {
  super.onStop();

  // ATTENTION: This was auto-generated to implement the App Indexing API.
  // See https://g.co/AppIndexing/AndroidStudio for more information.
  Action viewAction = Action.newAction(
   Action.TYPE_VIEW, // TODO: choose an action type.
   "Main Page", // TODO: Define a title for the content shown.
   // TODO: If you have web page content that matches this app activity's content,
   // make sure this auto-generated web page URL is correct.
   // Otherwise, set the URL to null.
   Uri.parse("http://host/path"),
   // TODO: Make sure this auto-generated app deep link URI is correct.
   Uri.parse("android-app://com.example.android.fragmentactivity/http/host/path")
  );
  AppIndex.AppIndexApi.end(client, viewAction);
  client.disconnect();
 }

 /**
  * 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_main, container, false);


   String[] forecastArray = {
    "Today , Sunny , 88/63",
    "Tomorrow , Sunny , 88/63",
    "Wesneday , Sunny , 88/63",
    "THursday , Sunny , 88/63",
    "FRIDAY , Sunny , 88/63",
    "Saturday , Sunny , 88/63",
    "Sunday , Sunny , 88/63",
   };

   List < String > weekForecast = new ArrayList < String > (
    Arrays.asList(forecastArray)
   );

   ArrayAdapter < String > mForecastAdapter;
   mForecastAdapter = new ArrayAdapter < String > (
    getActivity(),
    R.layout.list_item_forecast,
    android.R.id.text1, weekForecast);
   ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
   listView.setAdapter(mForecastAdapter);
   return rootView;

  }

 }
}

1 个答案:

答案 0 :(得分:3)

找不到您使用id = container

定义的布局

检查您的xml并验证ID是否相同

<YourLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        ... 
        android:id="@+id/container"
        >

</YourLayout>

就像ChrisStillwell在评论中建议的那样,如果这是最近的变化,有时重建可以解决问题。