启动新活动 Android 时出错

Posted

技术标签:

【中文标题】启动新活动 Android 时出错【英文标题】:Error when starting a new activity Android 【发布时间】:2014-04-14 07:18:58 【问题描述】:

我是 android 开发新手,在创建新的 Activity 时遇到了一些问题。当单击Button 但应用程序崩溃时,我想开始一个新的Activity。以下是控制台和 logcat 中的内容:

LogCat

03-11 00:21:08.639: D/AndroidRuntime(336): 关闭 VM
03-11 00:21:08.649: W/dalvikvm(336): threadid=1: 线程以未捕获的异常退出(组=0x40015560)
03-11 00:21:08.659:E/AndroidRuntime(336):致命异常:主要
03-11 00:21:08.659: E/AndroidRuntime(336): java.lang.RuntimeException: 无法启动活动 ComponentInfocom.src.vicnote/com.src.vicnote.MainActivity: java.lang.NullPointerException
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.os.Handler.dispatchMessage(Handler.java:99)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.os.Looper.loop(Looper.java:123)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 java.lang.reflect.Method.invokeNative(Native Method)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 java.lang.reflect.Method.invoke(Method.java:507)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 00:21:08.659: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
03-11 00:21:08.659: E/AndroidRuntime(336): 由: java.lang.NullPointerException 引起
03-11 00:21:08.659: E/AndroidRuntime(336): 在 com.src.vicnote.MainActivity.onCreate(MainActivity.java:26)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 00:21:08.659: E/AndroidRuntime(336): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 00:21:08.659: E/AndroidRuntime(336): ... 11 更多
03-11 00:21:38.579: D/AndroidRuntime(365): 关闭虚拟机
03-11 00:21:38.579: W/dalvikvm(365): threadid=1: 线程以未捕获的异常退出(组=0x40015560)
03-11 00:21:38.599:E/AndroidRuntime(365):致命异常:主要
03-11 00:21:38.599: E/AndroidRuntime(365): java.lang.RuntimeException: 无法启动活动 ComponentInfocom.src.vicnote/com.src.vicnote.MainActivity: java.lang.NullPointerException
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.os.Handler.dispatchMessage(Handler.java:99)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.os.Looper.loop(Looper.java:123)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 java.lang.reflect.Method.invokeNative(Native Method)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 java.lang.reflect.Method.invoke(Method.java:507)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 00:21:38.599: E/AndroidRuntime(365): at dalvik.system.NativeStart.main(Native Method)
03-11 00:21:38.599: E/AndroidRuntime(365): 由: java.lang.NullPointerException 引起
03-11 00:21:38.599: E/AndroidRuntime(365): 在 com.src.vicnote.MainActivity.onCreate(MainActivity.java:26)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 00:21:38.599: E/AndroidRuntime(365): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 00:21:38.599: E/AndroidRuntime(365): ... 11 更多

activity_main

<pre><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_
android:layout_
tools:context="com.src.vicnote.MainActivity"
tools:ignore="MergeRootFrame" /><code>

fragment_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/MainScreen" android:layout_ android:layout_ 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.src.vicnote.MainActivity$PlaceholderFragment" > <Button android:id="@+id/buttonNew" style="?android:attr/buttonStyleSmall" android:layout_ android:layout_ android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="New" /> </RelativeLayout>

Java 代码

http://pastebin.com/K8eAWD2L

【问题讨论】:

刚启动就崩溃了? 它说你的按钮是空的 你能发布你的xml吗?看来buttonNewactivity_main.xml 中不是有效的id 发布activity_main.xml文件 我正在尝试修复这个丑陋的帖子,很抱歉。是的,JRowan,它在启动时崩溃了。谢谢你的回复:) 【参考方案1】:

从外观上看是您设置内容视图

setContentView(R.layout.activity_main);

但是你提供的activity_main.xml中没有按钮,它显示一个空指针异常,因为

newButton = (Button) findViewById(R.id.buttonNew);

为空,因为您提供的信息中的实际按钮位于

fragment_main.xml

你可以试试

setContentView(R.layout.fragment_main);

【讨论】:

【参考方案2】:

检查您的 Manifest.xml 并确保你有一些国王

<activity android:name=".Mainfragment" />

【讨论】:

【参考方案3】:

您的 xml 中可能缺少声明

很有可能

R.id.buttonNew

澄清一下:您的活动正在寻找一个名为 buttonNew 的按钮,但没有找到它

【讨论】:

不行,他应该把放在activity_main.xml中 我认为他宁愿在那里寻找正确的ID。 我修好了!非常感谢您..您是对的,我的按钮不在正确的 xml 文件中。再次感谢你:) 很高兴为您提供帮助。如果您认为我回答了您的问题,请将其标记为正确答案。【参考方案4】:
here you have to mention your button in fragment_main.xml and you have to set 

activity_main.xml 在 setContentView() 方法中,那么如何找到你的 按钮只是改变xml文件名。

如下:

setContentView(R.layout.fragment_main);

【讨论】:

请改进您的答案。格式对内容根本没有帮助。这真是不合标准的输入......【参考方案5】:
First of all , I read your all code ...Do you know, what u did big mistake..

In Activity

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

            newButton = (Button) findViewById(R.id.buttonNew);

Can you check out , where your define button id in (R.layout.activity_main)

Second Mistake in Fragment

getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment())
                        .commit();
you have to call

getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();

【讨论】:

【参考方案6】:
package com.mvvmmodelrecyclerview;

import android.app.Activity;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;

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



public class MainActivity extends Activity 


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);




        //FragmentManager manager = getSupportFragmentManager();
        if (savedInstanceState == null) 

            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();




        


    




    /**
     * A placeholder fragment containing a simple view.
     */

    public static class PlaceholderFragment extends Fragment 
        Button newButton;
        public PlaceholderFragment() 
        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            newButton = (Button) rootView.findViewById(R.id.buttonNew);
            newButton.setOnClickListener(new View.OnClickListener() 

                @Override
                public void onClick(View v) 
                    // TODO Auto-generated method stub

                    Intent newNote = new Intent(getActivity(), NewNoteActivity.class);
                    startActivity(newNote);
                
            );
            return rootView;
        

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) 

            super.onActivityCreated(savedInstanceState);
        
    



activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_
    android:layout_
    android:orientation="vertical"
    >


    <FrameLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_
        android:layout_
        tools:context="com.src.vicnote.MainActivity"
        tools:ignore="MergeRootFrame" />
</LinearLayout>

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_
    android:layout_
    >
    <Button
        android:layout_gravity="center"
        android:id="@+id/buttonNew"
        style="?android:attr/buttonStyleSmall"
        android:layout_
        android:layout_
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="New" />
    <TextView
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_
        android:layout_
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Fragmnt View" />

</LinearLayout>

【讨论】:

【参考方案7】:
Or if you want to add button in activity_main.xml 

just as like your code did...then try to do it proper way...like that...


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_
    android:layout_
    android:orientation="vertical"
    >

    <Button
        android:layout_gravity="center"
        android:id="@+id/buttonNew"
        style="?android:attr/buttonStyleSmall"
        android:layout_
        android:layout_
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="New" />
    <FrameLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_
        android:layout_
        tools:context="com.src.vicnote.MainActivity"
        tools:ignore="MergeRootFrame" />
</LinearLayout>


fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_
    android:layout_
    >

    <TextView
        android:gravity="center_vertical"
        android:layout_gravity="center"
        android:layout_
        android:layout_
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Fragmnt View" />

</LinearLayout>



MainActivity.java

package com.mvvmmodelrecyclerview;

import android.app.Activity;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;

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



public class MainActivity extends Activity 

    Button newButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        newButton = (Button) findViewById(R.id.buttonNew);
        newButton.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                // TODO Auto-generated method stub

                Intent newNote = new Intent(MainActivity.this, NewNoteActivity.class);
                startActivity(newNote);
            
        );


        //FragmentManager manager = getSupportFragmentManager();
        if (savedInstanceState == null) 

            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();




        


    




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

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) 
            super.onActivityCreated(savedInstanceState);
        
    



【讨论】:

以上是关于启动新活动 Android 时出错的主要内容,如果未能解决你的问题,请参考以下文章

启动 Android 活动时出错

进入新活动 Android 时出错

创建新的 android 活动时出错

安装应用程序后在 android studio 中启动活动时出错

错误类型 3 - 启动活动时出错

无法启动活动 ComponentInfo 二进制 XML 文件第 13 行:膨胀类 com.google.android.material.appbar.MaterialToolbar 时出错