2014-03-31 74 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

相关问题