2014-03-31 79 views
1

R.Java失蹤結束數小時後,還有幾十個其他隨機問題,我終於找到了兩個(相關的)錯誤 - 但我無法修復它們。我查了很多次,但在這個網站或任何其他網站上沒有關於此問題的其他帖子。這裏是我的MainActivity.java:Eclipse Android應用程序「PlaceholderFragment無法解析爲某種類型」錯誤

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 { 

我已經試過項目 - >清潔和項目 - >生成的所有多次,甚至重新啓動Eclipse客戶端,但問題仍然存在。我以前從來沒有遇到這個問題(在舊的測試應用程序或這個測試應用程序中),儘管沒有一個編碼是不同的。如果您需要更多我的代碼,只需詢問,然後我將粘貼它。同樣,如果您需要解釋給定代碼的一部分(由於評論不足),請詢問我將盡力向您解釋。我懷疑我能解釋大部分內容 - 畢竟,我仍然在學習這門語言。

事後看來,'佔位符'的使用表明我輸入自己的名字 - 但這在the guide I've been following中從未解釋過。如果我試圖更加定製它,它可能會比以前更加破碎。如果問題確實如此簡單,我希望有人能夠引導我完成我應該採取的步驟!

+0

你在代碼中放錯了'}'。你使用的IDE,所以應該很容易修復 – Raghunandan

回答

1

你有一個錯位的}在你的代碼

@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 

而在代碼末尾刪除多餘的}

+0

真棒,它的工作!非常感謝。 :D我永遠不會看到這個問題,因爲它從未被指出是一個錯誤。我的操作欄圖標仍然不起作用,這非常令人失望,但它運行 - 這是重要的! – Jedibrad

+0

一旦我發現它已經工作,我試圖接受它,但在提問之後的10分鐘內我不能接受答案。你現在被標記爲答案。 :) – Jedibrad

+0

@ user3479544這是因爲該網站有限制一個計時器。 – Raghunandan

相關問題