应用程序在启动时崩溃,向mainActivity添加按钮代码

时间:2014-06-04 09:56:29

标签: java android xml button navigation-drawer

我的应用程序崩溃,当我将代码添加到主要活动后单击按钮并将结果从一个文本框输出到另一个文本框后启动它。如果没有按钮代码,它将运行grand并在启动时打开导航抽屉。任何帮助将不胜感激。

logcat的

06-04 10:05:47.625: E/AndroidRuntime(17131): FATAL EXCEPTION: main
06-04 10:05:47.625: E/AndroidRuntime(17131): Process: com.draco.dragonmessage, PID: 17131
06-04 10:05:47.625: E/AndroidRuntime(17131): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.draco.dragonmessage/com.draco.dragonmessage.MainActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.os.Looper.loop(Looper.java:136)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.ActivityThread.main(ActivityThread.java:5144)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at java.lang.reflect.Method.invoke(Method.java:515)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at dalvik.system.NativeStart.main(Native Method)
06-04 10:05:47.625: E/AndroidRuntime(17131): Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at com.draco.dragonmessage.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:145)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.Fragment.performCreateView(Fragment.java:1700)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.Activity.performStart(Activity.java:5240)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
06-04 10:05:47.625: E/AndroidRuntime(17131):    ... 11 more
06-04 10:05:47.625: E/AndroidRuntime(17131): Caused by: java.lang.IllegalArgumentException: Binary XML file line #31: Duplicate id 0x7f080002, tag null, or parent id 0x7f080000 with another fragment for com.draco.dragonmessage.NavigationDrawerFragment
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.app.Activity.onCreateView(Activity.java:4791)
06-04 10:05:47.625: E/AndroidRuntime(17131):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-04 10:05:47.625: E/AndroidRuntime(17131):    ... 22 more
06-04 10:40:57.015: D/ActivityThread(18803): handleBindApplication:com.draco.dragonmessage
06-04 10:40:57.015: D/ActivityThread(18803): setTargetHeapUtilization:0.25
06-04 10:40:57.015: D/ActivityThread(18803): setTargetHeapMinFree:524288
06-04 10:40:57.245: D/AndroidRuntime(18803): Shutting down VM
06-04 10:40:57.245: W/dalvikvm(18803): threadid=1: thread exiting with uncaught exception (group=0x41841ce0)
06-04 10:40:57.245: E/AndroidRuntime(18803): FATAL EXCEPTION: main
06-04 10:40:57.245: E/AndroidRuntime(18803): Process: com.draco.dragonmessage, PID: 18803
06-04 10:40:57.245: E/AndroidRuntime(18803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.draco.dragonmessage/com.draco.dragonmessage.MainActivity}: java.lang.NullPointerException
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.os.Looper.loop(Looper.java:136)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.ActivityThread.main(ActivityThread.java:5144)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at java.lang.reflect.Method.invoke(Method.java:515)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at dalvik.system.NativeStart.main(Native Method)
06-04 10:40:57.245: E/AndroidRuntime(18803): Caused by: java.lang.NullPointerException
06-04 10:40:57.245: E/AndroidRuntime(18803):    at com.draco.dragonmessage.MainActivity.onCreate(MainActivity.java:48)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.Activity.performCreate(Activity.java:5231)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-04 10:40:57.245: E/AndroidRuntime(18803):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
06-04 10:40:57.245: E/AndroidRuntime(18803):    ... 11 more

MainActivity

package com.draco.dragonmessage;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;

public class MainActivity extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;
    String sendMessageString = "";

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

       final Button sendMessage = (Button) findViewById(R.id.messageButton); 
       System.out.println(sendMessage.toString());
            sendMessage.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                final TextView receivedMessage = (TextView) findViewById(R.id.textReceived);
                final EditText messageToSend = (EditText) findViewById(R.id.editTextSend);
                sendMessageString = messageToSend.toString();
                receivedMessage.setText(sendMessageString);
            }
        });

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        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.
        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 {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }
    }

main_activity.xml

<!-- A DrawerLayout is intended to be used as the top-level content view using                match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.draco.dragonmessage.MainActivity" >

<!--
     As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions.
-->

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!--
     android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead.
-->
<!--
     The drawer is given a fixed width in dp and extends the full height of
     the container.
-->

<fragment
    android:id="@+id/navigation_drawer"
    android:name="com.draco.dragonmessage.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start" />

</android.support.v4.widget.DrawerLayout>

fragment_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.draco.dragonmessage.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/editTextSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/section_label"
        android:layout_alignParentBottom="true"
        android:ems="10"
        android:hint="Enter message to send..." />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/section_label"
        android:layout_alignTop="@+id/textReceived"
        android:text="Keith"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textReceived"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/section_label"
        android:layout_alignRight="@+id/editTextSend"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/messageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editTextSend"
        android:layout_alignParentRight="true"
        android:layout_marginRight="31dp"
        android:text="Button" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

在你的onCreateView中

你正在膨胀错误的布局。这个

 View rootView = inflater.inflate(R.layout.activity_main, container, false);

应该是

 View rootView = inflater.inflate(R.layout.fragment_activity, container, false);