mainActivity中的按钮在第二个活动中打开一个片段

时间:2013-11-13 14:30:57

标签: android android-intent android-activity android-fragments android-fragmentactivity

我想在我的mainactivity中按下一个按钮,该按钮将启动一个带有片段的新活动的意图。 我用MainActivity的按钮和带片段的second_activity.xml写了一个main_activity.xml。当我点击按钮时,由于xml中的片段,第二个Activity将崩溃。 我已经添加了一个标志,但它没有帮助。 该应用程序的想法是通过按下主要活动中的按钮来获取第二个活动中的googlemap。

我希望你能帮助我:/

MainActivity:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private Button mButton;

    private static final String TAG = Thread.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "+++++ onCreate() +++++");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.mapButton);
        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "+++++ onClick() +++++");
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this, SubMain.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

    }

    protected void onStart() {
        Log.d(TAG, "+++++ onStart() +++++");
        super.onStart();

    }

    protected void onPause() {
        Log.d(TAG, "+++++ onPause() +++++");
        super.onPause();

    }

    @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 void onClick(View v) {
        // TODO Auto-generated method stub
    }
}

子活动:

public class SubMain extends MainActivity {

    private static final String TAG = SubMain.class.getSimpleName();

    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "+++++ onCreate().SubMain +++++");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.submain);
    }

}

activity_main.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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
         />

    <Button
        android:id="@+id/mapButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Map" />

</RelativeLayout>

submain.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

LogCat:

11-13 14:48:24.395: D/Thread(30503): +++++ onCreate() +++++
11-13 14:48:24.465: D/Thread(30503): +++++ onStart() +++++
11-13 14:48:24.475: D/Thread(30503): +++++ run() +++++
11-13 14:48:24.480: D/Thread(30503): Thread is running
11-13 14:48:24.980: D/Thread(30503): Thread is running
11-13 14:48:25.485: D/Thread(30503): Thread is running
11-13 14:48:25.985: D/Thread(30503): Thread is running
11-13 14:48:26.485: D/Thread(30503): Thread is running
11-13 14:48:26.985: D/Thread(30503): Thread is running
11-13 14:48:27.240: D/Thread(30503): +++++ onClick() +++++
11-13 14:48:27.270: D/Thread(30503): +++++ onPause() +++++
11-13 14:48:27.305: D/SubMain(30503): +++++ onCreate().SubMain +++++
11-13 14:48:27.305: D/Thread(30503): +++++ onCreate() +++++
11-13 14:48:27.320: D/AndroidRuntime(30503): Shutting down VM
11-13 14:48:27.320: W/dalvikvm(30503): threadid=1: thread exiting with uncaught exception (group=0x41da92a0)
11-13 14:48:27.345: E/AndroidRuntime(30503): FATAL EXCEPTION: main
11-13 14:48:27.345: E/AndroidRuntime(30503): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thread/com.example.thread.SubMain}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.os.Looper.loop(Looper.java:137)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at java.lang.reflect.Method.invoke(Method.java:511)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at dalvik.system.NativeStart.main(Native Method)
11-13 14:48:27.345: E/AndroidRuntime(30503): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.app.Activity.setContentView(Activity.java:1924)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at com.example.thread.SubMain.onCreate(SubMain.java:13)
11-13 14:48:27.345: E/AndroidRuntime(30503):    at android.app.Activity.performCreate(Activity.java:5206)

1 个答案:

答案 0 :(得分:0)

问题解决了: 我在android清单中得到了错误的元数据...这是一个非常愚蠢的错误。 &GT;。&LT;

Bevor:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.thread"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.thread.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.thread.SubMain"
            android:label="@string/app_name" >
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyCpP0eOduM6JF6w8i7xybZgIiZopG4tOVI" /> <!-- wrong line! -->
        </activity>
    </application>

</manifest>

之后:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.thread"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.thread.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.thread.SubMain"
            android:label="@string/app_name" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="MY_GOOGLE_API_KEY" />
    </application>

</manifest>