第一课安卓

时间:2014-06-07 02:25:06

标签: java android eclipse android-intent

当我开始在Android设备中应用程序时,应用程序在单击inly按钮后崩溃,这意味着带来下一个活动的意图不起作用但我不知道我的错误在哪里,我确定我拼错了什么,但在哪里?

06-06 21:00:13.692: E/AndroidRuntime(18154): FATAL EXCEPTION: main
06-06 21:00:13.692: E/AndroidRuntime(18154): Process: com.example.myfirstapp, PID:     18154
06-06 21:00:13.692: E/AndroidRuntime(18154): java.lang.RuntimeException: Unable to     start activity     ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}:     java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41ad8eb0 #0 id=0x7f05003c}
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.os.Looper.loop(Looper.java:136)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.ActivityThread.main(ActivityThread.java:5081)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at java.lang.reflect.Method.invokeNative(Native Method)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at java.lang.reflect.Method.invoke(Method.java:515)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at dalvik.system.NativeStart.main(Native Method)
06-06 21:00:13.692: E/AndroidRuntime(18154): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41ad8eb0 #0 id=0x7f05003c}
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.Activity.performStart(Activity.java:5241)
06-06 21:00:13.692: E/AndroidRuntime(18154):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
06-06 21:00:13.692: E/AndroidRuntime(18154):    ... 11 more
06-06 21:00:16.125: I/Process(18154): Sending signal. PID: 18154 SIG: 9

这是MainActivity 我认为这是错误,但我不知道在哪里:

package com.example.myfirstapp;

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.widget.EditText;
import android.os.Build;
import android.content.Intent;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @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);
}
public void sendMessage (View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

/**
 * 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;
    }
}

}

如果存在有用的东西,则显示清单:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.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.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

尝试更改此行并检查其是否有效。

setContentView(R.layout.activity_main);

setContentView(com.example.myfirstapp.R.layout.activity_main);

因为android包中有R.java,如果你意外导入了它会产生错误。

检查这是否是原因,如果没有,那么我们可以进一步诊断。

修改:java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41ad8eb0 #0 id=0x7f05003c}

此行指示PlaceholderFragment中的问题。所以问题可能出在这段代码中

if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

您尝试将PlaceholderFragment添加到R.id.container