无法在每个 activity 上解析符号 'R'
Cannot resolve symbol 'R' on every activity
我做了一个小应用,一个动作可以调用两个片段。即使 android studio 标记每个 findViewById(R.id.Example) 阅读,这也绝对正常,这显然意味着有问题。只有 "R" 标记为红色。但我不明白哪里出了问题?特别是当应用程序运行良好并且编译没有给出任何错误时。
这里是主要的activity:
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.");
}
}
您只需要重建然后清理您的项目。这只是 IDE 本身的构建问题。给它一个 Build,一个 clean 然后你就可以开始了。
希望这有帮助
我遇到了类似的问题,我是这样做的:
清理项目并与 Gradle
同步项目
R 是根据您的 xml 文件生成的。检查您的布局中是否存在任何阻止生成 R 的错误。
我做了一个小应用,一个动作可以调用两个片段。即使 android studio 标记每个 findViewById(R.id.Example) 阅读,这也绝对正常,这显然意味着有问题。只有 "R" 标记为红色。但我不明白哪里出了问题?特别是当应用程序运行良好并且编译没有给出任何错误时。
这里是主要的activity:
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.");
}
}
您只需要重建然后清理您的项目。这只是 IDE 本身的构建问题。给它一个 Build,一个 clean 然后你就可以开始了。 希望这有帮助
我遇到了类似的问题,我是这样做的:
清理项目并与 Gradle
同步项目R 是根据您的 xml 文件生成的。检查您的布局中是否存在任何阻止生成 R 的错误。