我无法从当前活动转移到另一个活动

时间:2014-05-03 04:56:34

标签: java android button android-activity

当我尝试点击MainActivity中的新活动按钮时,我的应用程序崩溃并关闭。在屏幕上显示此错误“遗憾[应用程序名称]已停止” 我谷歌这个问题,我找不到任何明确的解决方案。

这里是logCat Log,First Java Source(MainActivity.java)和seccond java source(Seccond.java)。

Logcat日志:

05-03 00:05:17.750: E/AndroidRuntime(1091): FATAL EXCEPTION: main
05-03 00:05:17.750: E/AndroidRuntime(1091): Process: com.example.helloworld, PID: 1091
05-03 00:05:17.750: E/AndroidRuntime(1091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.Seccond}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.os.Looper.loop(Looper.java:136)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at java.lang.reflect.Method.invoke(Method.java:515)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at dalvik.system.NativeStart.main(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091):     Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.Activity.performStart(Activity.java:5241)
05-03 00:05:17.750: E/AndroidRuntime(1091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)

MainActivity.java

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


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

        // Start

        Button btn_img = (Button) findViewById(R.id.btn_image);
        Button btn = (Button) findViewById(R.id.btn_Click);
        final TextView txt = (TextView) findViewById(R.id.txt_view);
        Button btn_avtivity = ( Button) findViewById(R.id.btn_activity);


        // For button click
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                txt.setText(" Amin Bassam ");
            }
        });

            // Image show
        btn_img.setOnClickListener(new View.OnClickListener() {

            @Override  
            public void onClick(View arg0) {
                   ImageView img=(ImageView)findViewById(R.id.img_view);
                   img.setImageResource(R.drawable.amin);
            }
        });

        btn_avtivity.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(getApplicationContext(),Seccond.class);
                    startActivity(intent);

            }
        });

            // Open Activities
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new Fragment()).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.
     */
}

Seccond.java

package com.example.helloworld;

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 Seccond extends ActionBarActivity {

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

        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.seccond, 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_seccond,
                    container, false);
            return rootView;
        }
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.helloworld.MainActivity"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/txt_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_Click"
        android:layout_alignParentTop="true"
        android:layout_marginTop="98dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btn_Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="66dp"
        android:text="Click"
        tools:ignore="HardcodedText" />
"res/layout/activity_main.xml"
    <Button
        android:id="@+id/btn_image"
        style="@style/AppTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn_Click"
        android:layout_alignBottom="@+id/btn_Click"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/btn_Click"
        android:text="Image"
        tools:ignore="HardcodedText" />

    <ImageView
        android:id="@+id/img_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_image"
        android:layout_centerVertical="true"
        android:scaleType="center"
        android:src="@drawable/abc_ab_solid_light_holo"
        tools:ignore="ContentDescription" />

    <Button
        android:id="@+id/btn_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/btn_image"
        android:text="New Activities"
        tools:ignore="HardcodedText" />

</RelativeLayout>

Seccond_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:background="#A8B007"
    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.example.helloworld.Seccond$PlaceholderFragment" >

</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    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.helloworld.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.helloworld.Seccond"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

4 个答案:

答案 0 :(得分:1)

No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment

错误是说它无法找到容器ID为片段占位符的视图。

  

的setContentView(R.layout.activity_seccond);

     

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

这部分代码是主要问题。在getSupportFragmentManager的add()中,它表示创建片段并将其放在具有id容器的视图中。但是setContentView中指定的布局没有id为container的视图。因此需要一个带有视图(如框架)的布局文件,其id为容器,这样当片段布局加载时,它就可以进入该视图。

尝试使用以下命令将文件添加到名为activity_frame_seccond.xml的布局中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这是一个带有id&#39;容器&#39;的框架。

在Seccond.java中更改此行:

setContentView(R.layout.activity_seccond);

代表

setContentView(R.layout.activity_frame_seccond);

看看是否有效。

使用此代码,您将使用新文件activity_frame_seccond.xml作为活动的布局,将第一个文件activity_seccond.xml用作将放入框架元素的片段(因为它具有id容器)。

如果这不起作用,请告诉我,我会再看看。

答案 1 :(得分:0)

检查Fragment类中的这一行:

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

我认为返回的视图是空的

答案 2 :(得分:0)

从清单中删除此行

 <activity
            android:name="com.example.helloworld.Second"
            android:label="@string/app_name" >
            >
</activity>

并尝试

答案 3 :(得分:-1)

以这种方式尝试

 @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(this,Seccond.class);
                startActivity(intent);

        }