gpt4 book ai didi

java - Eclipse Android 应用程序 "PlaceholderFragment cannot be resolved to a type"错误

转载 作者:行者123 更新时间:2023-11-30 02:59:35 24 4
gpt4 key购买 nike

经过数小时的 R.java 消失以及许多其他随机问题,我终于找到了两个(相关的)错误 - 但我无法修复它们。我已经查过很多次了,但是在这个网站或任何其他网站上都没有关于这个问题的其他帖子。这是我的 MainActivity.java:

package com.yahoo.jedibradftw.audiobooksync;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends ActionBarActivity {

@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();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}

@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.
switch (item.getItemId()) {
case R.id.action_search:
// search
return true;
case R.id.action_settings:
// settings
return true;
default:
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_main, container,
false);
return rootView;
}
}

}}

报错如下:

PlaceholderFragment 无法解析为类型。第 21 行:

.add(R.id.container, new PlaceholderFragment()).commit();

局部类 PlaceholderFragment 的非法修饰符;只允许摘要或最终。第 52 行:

public static class PlaceholderFragment extends Fragment {

多次尝试Project -> Clean和Project -> Build All,甚至重启了Eclipse客户端,问题依旧。我以前从未遇到过这个问题(在旧的测试应用程序或这个应用程序中),即使没有任何编码不同。如果您需要我的更多代码,请询问,我会将其粘贴。同样,如果您需要解释给定代码的一部分(由于注释不充分),请询问,我会尽力向您解释。不过,我怀疑我能否解释其中的大部分内容 - 毕竟我仍在学习这门语言。

事后看来,“占位符”的使用表明我输入了自己的名字——但这从未在 the guide I've been following 中解释过.如果我尝试更多地自定义它,它最终可能会比以前更坏。如果问题真的这么简单,我希望有人能指导我完成我应该采取的步骤!

最佳答案

您的代码中放错了

@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.
switch (item.getItemId()) {
case R.id.action_search:
// search
return true;
case R.id.action_settings:
// settings
return true;
default:
return super.onOptionsItemSelected(item);
}

更改为

@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.
switch (item.getItemId()) {
case R.id.action_search:
// search
return true;
case R.id.action_settings:
// settings
return true;
default:
return super.onOptionsItemSelected(item);
}
} // missing

并删除代码末尾多余的}

关于java - Eclipse Android 应用程序 "PlaceholderFragment cannot be resolved to a type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com