我按照本教程中的说明做了所有事情: google android basic tutorial 尽管一切都按照描述完成,但代码拒绝编译并出现 3 个错误。看起来写 turorial 的人忘了提到那些东西是什么以及我在哪里/如何定义它们。
我得到的错误:
Error:(24, 68) error: cannot find symbol variable container
Error:(36, 23) error: cannot find symbol variable action_settings
Error:(46, 54) error: cannot find symbol variable fragment_display_message
这 3 个字段都没有在任何地方定义(也许其中一个库是错误的?) 有问题的文件是:
package com.example.asteroth.first;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.R;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
// setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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);
}
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_display_message, container, false);
return rootView;
}
}
}
我正在使用我刚刚下载的 Android Studio,搜索或类似问题中没有任何问题指向像这样的问题,因此我怀疑教程的作者忘记提及一些小问题。我看到有人建议将“容器”作为新 ID 放入其中一个 XML 文件中,但无济于事。
编辑: 'cannot find symbol ActionBarActivity' following Android Development Tutorial? 这篇文章提出了一个解决方案,但是它将 ActionBarActivity 更改为 Activity,这与教程使用的非常不同,我不知道它会造成多严重的影响
编辑2: 发现并移除的问题: import android.R//导致action_settings错误 缺少容器//必须将其作为 id 添加到 xml 文件中 xml file named wrong//如果我没记错,我还在等待有经验的人来澄清,但教程似乎使用了与 java 代码引用的 xml 文件不同的名称
剩下的问题和这个类似 Cannot resolve method placeholderfragment error 但是,我既扩展了 Fragment 又包含了 android.app.Fragment,如包含文件中所示。
最佳答案
我尝试了相同的教程,下面是我如何修复错误:
R.id.container 无法解析错误
我必须导入 android.support.v4.app.Fragment 来解决这个问题,并将 android:id = "@+id/container"添加到我的 activity_display_message.xml 文件中的 RelativeLayout 部分。
无法解析 fragment_display_message 错误
改为将 R.layout.fragment_display_message 改为 R.layout.activity_display_message。无需为 fragment_display_message 创建新的 xml 文件。
这应该可以解决这两个错误。
但是如果你注释掉 if(savedInstanceState.......... 语句,你可能会更好,否则你的程序会在你尝试运行它时崩溃,如果它不给你任何错误。
您的 onCreate 方法应如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView=new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
/*if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}*/
}
关于android - 谷歌 Android 教程 - 不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799727/