Java 无法解析符号';R';在每一项活动上

Java 无法解析符号';R';在每一项活动上,java,android,android-layout,android-fragments,findviewbyid,Java,Android,Android Layout,Android Fragments,Findviewbyid,我制作了一个小应用程序,它的动作可以称为两个片段。即使android studio标记每一个findViewById(R.id.Example)read,这也绝对有效,这显然意味着有问题。只有“R”标记为红色。但是我不明白怎么了?尤其是当应用程序运行良好,编译没有给出任何错误时 以下是主要活动: package silverbeach.meintieralter; import android.app.Fragment; import android.app.FragmentTransacti

我制作了一个小应用程序,它的动作可以称为两个片段。即使android studio标记每一个findViewById(R.id.Example)read,这也绝对有效,这显然意味着有问题。只有“R”标记为红色。但是我不明白怎么了?尤其是当应用程序运行良好,编译没有给出任何错误时

以下是主要活动:

package silverbeach.meintieralter;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    private FragmentOne fragmentOne;
    private FragmentTwo fragmentTwo;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        //Fragmente ankündigen
        fragmentOne = (FragmentOne) Fragment.instantiate(this, FragmentOne.class.getName(),null);
        fragmentTwo = (FragmentTwo) Fragment.instantiate(this, FragmentTwo.class.getName(),null);


        //Fragment 1 wird STANDARTMÄSSIG eingeblendet und ausgeführt
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.flFragmentContainer, fragmentOne);
        fragmentTransaction.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.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.action_one:{
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.flFragmentContainer, fragmentOne);
                fragmentTransaction.commit();
                break;
            }
            case R.id.action_two:{
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.flFragmentContainer, fragmentTwo);
                fragmentTransaction.commit();
                break;
            }
            default:{

            }
        }

        return super.onOptionsItemSelected(item);
    }
}
这是其中一个片段:

package silverbeach.meintieralter;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.View.OnClickListener;

import static android.content.Context.VIBRATOR_SERVICE;


public class FragmentOne extends Fragment {

    private TextView out;
    private EditText alter;
    private Spinner s;
    private Button button;
    private FloatingActionButton fab;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View layout = inflater.inflate(R.layout.fragment_one, null);

        out = (TextView) layout.findViewById(R.id.out);
        alter = (EditText) layout.findViewById(R.id.altereingabe);
        s = (Spinner) layout.findViewById(R.id.spinner);
        button = (Button) layout.findViewById(R.id.button);
        fab = (FloatingActionButton) layout.findViewById(R.id.fab);



        FloatingActionButton fab = (FloatingActionButton) layout.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
                                      @Override
                                      public void onClick(View v) {




                                          String text = s.getSelectedItem().toString();
                                          int newage = 404;
                                          double doublenewage = 4.04;
                                          double doubleage;
                                          int intage;
                                          doubleage = Double.parseDouble(alter.getText().toString());
                                          intage = (int) doubleage;

                                          switch (text) {
                                              case "Katze": {
                                                  switch (intage) {

                                                      default:
                                                          Snackbar.make(v, "Leider schon tot.", Snackbar.LENGTH_LONG).show();
                                                          out.setText("Leider schon tot.");
                                                          break;


                                                  }
                                                  if (intage <= 119)
                                                      Snackbar.make(v, "Du bist eine " + Integer.toString(newage) + " Jahre alte Katze.", Snackbar.LENGTH_LONG).show();

                                                  else {
                                                      double deadagecat;
                                                      deadagecat = intage * 0.283;
                                                      long dc = Math.round(deadagecat);
                                                      Snackbar.make(v, "Du bist höchstwahrscheinlich schon tot, aber theoretisch wärst du eine " + dc + "-Jährige Katze.", Snackbar.LENGTH_LONG).show();


                                                  }


                                              }break;
                                              case "Pferd": {
                                                  doublenewage = intage / 3.1;
                                                  long longpferdage = Math.round(doublenewage);
                                                  newage = (int) longpferdage;

                                                  if (newage < 31)
                                                      Snackbar.make(v, "Du bist ein " + Integer.toString(newage) + " Jahre altes Pferd", Snackbar.LENGTH_LONG).show();

                                                  else
                                                      Snackbar.make(v, "Du bist höchstwahrscheinlich schon tot, aber theoretisch wärst du ein " + newage + "-Jähriges Pferd.", Snackbar.LENGTH_LONG).show();

                                              }break;
                                              case "Hund": {
                                                  newage = intage / 7;
                                                  if (newage < 15)
                                                      Snackbar.make(v, "Du bist ein " + Integer.toString(newage) + " Jahre alter Hund.", Snackbar.LENGTH_LONG).show();

                                                  else
                                                      Snackbar.make(v, "Du bist höchstwahrscheinlich schon tot, aber theoretisch wärst du ein " + newage + "-Jähriger Hund.", Snackbar.LENGTH_LONG).show();


                                              }break;
                                              case "Meerschweinchen": {
                                                  newage = intage / 12;
                                                  if (newage < 8)

                                                      //out.setText("Du bist ein " + Integer.toString(newage) + " Jahre altes Meerie.");
                                                      Snackbar.make(v, "Du bist ein "+Integer.toString(newage)+" Jahre altes Meerie.", Snackbar.LENGTH_LONG).show();






                                                  else{
                                                  Snackbar.make(v, "Du bist höchstwahrscheinlich schon tot, aber theoretisch wärst du ein " + newage + "-Jähriges Meerie.", Snackbar.LENGTH_LONG).show();}


                                              }break;


                                          }



                                      }


                                  });
        // tvText1 = (TextView) layout.findViewById(R.id.tvFragmentOne);





        return layout;
    }





    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //tvText1.setText("Ok View 1 wurde geändert.");

    }
}
package silverbeach.meintierlater;
导入android.app.Fragment;
导入android.content.Context;
导入android.os.Bundle;
导入android.os.可控震源;
导入android.support.annotation.Nullable;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.view.view.OnClickListener;
导入静态android.content.Context.u服务;
公共类FragmentOne扩展了Fragment{
私有文本视图输出;
私人编辑文本修改;
私人纺纱机;
私人按钮;
私人浮动按钮制造厂;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图布局=充气机。充气(R.layout.fragment\u one,null);
out=(TextView)layout.findViewById(R.id.out);
alter=(EditText)layout.findViewById(R.id.altereingabe);
s=(微调器)layout.findviewbyd(R.id.Spinner);
按钮=(按钮)布局。findViewById(R.id.button);
fab=(FloatingActionButton)layout.findViewById(R.id.fab);
FloatingActionButton fab=(FloatingActionButton)layout.findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v){
String text=s.getSelectedItem().toString();
int newage=404;
双新年龄=4.04;
双倍年龄;
int intage;
doubleage=Double.parseDouble(alter.getText().toString());
摄入量=(整数)双倍年龄;
开关(文本){
案例“Katze”:{
开关(入口){
违约:
Snackbar.make(v,“Leider schon tot.”,Snackbar.LENGTH_LONG.show();
out.setText(“Leider schon tot”);
打破
}

如果(intage你只需要重新构建然后清理你的项目。这只是IDE本身的一个构建问题。给它一个构建,一个清理,然后你就可以开始了。
希望这对您有所帮助

您只需进行重建,然后清理项目。这只是IDE本身的一个构建问题。给它一个构建,一个清理,然后您就可以开始了。
希望这有帮助

我也有类似的问题,下面是我所做的:

清理项目并与Gradle同步项目


我也有类似的问题,下面是我所做的:

清理项目并与Gradle同步项目


R是基于xml文件生成的。请检查版面中是否存在阻止生成R的错误。

R是基于xml文件生成的。请检查版面中是否存在阻止生成R的错误。

请检查具有最高投票权的答案,也许它可以解决您的问题y更好。请检查投票率最高的答案,也许它能更好地解决你的问题。伙计,你救了我一命,直到我看到你的评论,我才想到这件讨厌的事,我的xml文件中有一个输入错误。但所有其他解决方案都失败了,我看到了评论。再次感谢你救了我一命,我没有想到这个讨厌的问题在看到您的评论之前,我的xml文件中有一个输入错误。但所有其他解决方案都失败了,我看到了您的评论。再次感谢