[英]Eclipse Android Application “PlaceholderFragment cannot be resolved to a type” Error
在經過數小時的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 and Project-> Build All,甚至重新啟動了Eclipse客戶端,但是問題仍然存在。 我以前從未遇到過這個問題(在舊的測試應用程序中或在此應用程序中),即使編碼沒有不同。 如果您需要更多我的代碼,請詢問,然后將其粘貼。類似地,如果您需要解釋給定代碼的一部分(由於注釋不足),請詢問,我將嘗試向您解釋。 我懷疑我能否解釋大部分內容-畢竟我仍在學習語言。
事后看來,“占位符”的使用表明我輸入了自己的名字-但這在我一直遵循的指南中從未得到解釋。 如果我嘗試對其進行更多自定義,則它可能最終會比以前更殘破。 如果問題確實如此簡單,我希望有人可以指導我完成我應該采取的步驟!
您的代碼中放錯了}
@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
並在代碼末尾刪除多余的}
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.