在eclipse中构建简单程序,但由于fragment_main而无法运行

时间:2014-03-29 20:12:00

标签: android eclipse android-fragments

我在Android上的开发者网站上使用该教程,因为片段main https://developer.android.com/training/basics/firstapp/building-ui.html< - 该教程,我无法让它运行。我不知道如何处理片段主!无论我怎么做,我都会在错误后得到错误。这里有什么帮助?我正在使用运行Lion的Mac。它也是在Linux上对我做的,这令人沮丧。

本教程没有提及fragment_main,如果我在没有主要活动的情况下构建项目,我缺少一半我需要的东西,如果我删除了fragment_main.xml,我会得到更多的错误。抱歉这个虚拟问题。我无法在任何地方找到答案。

请帮忙吗?

这些是我得到的错误

[2014-03-29 15:33:58 - FirstApp] Installation error: Unknown failure
[2014-03-29 15:33:58 - FirstApp] Please check logcat output for more details.
[2014-03-29 15:33:58 - FirstApp] Launch canceled!
[2014-03-29 15:59:19 - FirstApp] Error in an XML file: aborting build.
[2014-03-29 16:14:27 - FirstApp] Error in an XML file: aborting build.

这是MainActivity.Java

package com.example.firstapp;

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

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 true;
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        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.activity_main, container,
                    false);
            return rootView;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

package com.example.firstapp;

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

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @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 true;
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    }