【问题标题】:I can't move from current activity to another activity我无法从当前活动转移到另一个活动
【发布时间】:2014-05-03 04:56:33
【问题描述】:

当尝试在我的 MainActivity 中单击 New Activity Button 时,我的应用程序崩溃并关闭。屏幕上出现此错误“不幸的是[应用程序名称]已停止” 我用谷歌搜索这个问题,我找不到任何明确的解决方案。

这里是 logCat 日志,第一个 Java 源 (MainActivity.java) 和第二个 Java 源 (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>

【问题讨论】:

  • 也发布您的 xml 文件..
  • @Lal,发布我的主要和第二个 XML 文件。
  • View rootView = inflater.inflate(R.layout.activity_seccond,container, false); 这是哪里的activity_seccond ???
  • setContentView(R.layout.activity_seccond);layouts文件夹ie.activity_seccond.xml中有这样的xml文件吗???
  • 也发布你的 AndroidManifest.xml

标签: java android button android-activity


【解决方案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 的视图。所以需要有一个布局文件,里面有一个 id 为 container 的 View(就像一个框架),这样当 Fragment 布局加载时,它就可以进入那个视图。

尝试在布局中添加一个名为 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 为“容器”的框架。

在 Seccond.java 中更改这一行:

setContentView(R.layout.activity_seccond);

为

setContentView(R.layout.activity_frame_seccond);

看看这是否有效。

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

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

【讨论】:

  • @Lal 已编辑说明
  • @Arianne ,不幸的是,在我的项目中我删除了我的 fragment_[..].xml 。创建新项目,我的问题就消失了。但我的新问题是我的主要活动内容显示在我的 Seccond 活动中。谢谢你的完整回答。
【解决方案2】:

在你的 Fragment 类中检查这一行:

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

我认为返回的视图是空的

【讨论】:

  • 我的项目中没有片段类。
  • @A.Bassam: 那么这一行是什么:PlaceholderFragment extends Fragment
【解决方案3】:

从清单中删除此行

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

试试

【讨论】:

  • 将我的代码 [Second] 编辑为 [Seccond]。
  • 什么?我不明白..您在清单中指定了 Second 和 Seccond 活动..请从中删除 Second..
  • 这是我输入错误,我将第二个更改为第二个并删除添加行。
【解决方案4】:

这样试试

 @Override
        public void onClick(View arg0) {

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

        }

【讨论】:

  • 这个 Intent 有什么不同 intent = new Intent( CurrentLayout.this , Seccond.class );
  • 我认为没有区别
猜你喜欢
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
相关资源
最近更新 更多