无法用Fragment替换Activity

时间:2013-02-07 21:00:32

标签: android mobile nullpointerexception

当我点击开始按钮时,我希望我的活动切换到片段。当我尝试替换时,它给了我一个nullpointer异常。那是为什么?

我需要一些帮助,因为我很快就会完成这项任务。

package com.example.quizapp;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {
    private Fragment1 newFragment1;
    private FragmentTransaction ft;
    private Button startButton1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton1 = (Button) findViewById(R.id.startButton);
        startButton1.setOnClickListener(myhandler1);

        ft = getFragmentManager().beginTransaction();
        newFragment1 = (Fragment1) getFragmentManager().findFragmentById(R.layout.fragment1);

    }

    View.OnClickListener myhandler1 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ft.replace(R.layout.main, newFragment1);
        }
    };


    @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;
    }

}

这是logcat:

02-07 20:56:06.823: E/AndroidRuntime(1902): FATAL EXCEPTION: main
02-07 20:56:06.823: E/AndroidRuntime(1902): java.lang.NullPointerException
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.app.BackStackRecord.replace(BackStackRecord.java:429)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.app.BackStackRecord.replace(BackStackRecord.java:421)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at com.example.quizapp.MainActivity$1.onClick(MainActivity.java:35)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.view.View.performClick(View.java:4202)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.view.View$PerformClick.run(View.java:17340)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.os.Handler.handleCallback(Handler.java:725)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.os.Looper.loop(Looper.java:137)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at java.lang.reflect.Method.invoke(Method.java:511)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 20:56:06.823: E/AndroidRuntime(1902):     at dalvik.system.NativeStart.main(Native Method)

这是我的main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#FFF" >

    <fragment 
        android:name="com.example.quiz.Fragment1"
        android:id="@+id/fragment1"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/startButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="startButtonClicked"
        android:text="Start quiz!" />


</RelativeLayout>

在我说完你所说的之后,现在看看错误:

02-07 23:19:50.171: E/AndroidRuntime(869): FATAL EXCEPTION: main
02-07 23:19:50.171: E/AndroidRuntime(869): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quizapp/com.example.quizapp.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.os.Looper.loop(Looper.java:137)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 23:19:50.171: E/AndroidRuntime(869):  at java.lang.reflect.Method.invokeNative(Native Method)
02-07 23:19:50.171: E/AndroidRuntime(869):  at java.lang.reflect.Method.invoke(Method.java:511)
02-07 23:19:50.171: E/AndroidRuntime(869):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 23:19:50.171: E/AndroidRuntime(869):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 23:19:50.171: E/AndroidRuntime(869):  at dalvik.system.NativeStart.main(Native Method)
02-07 23:19:50.171: E/AndroidRuntime(869): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-07 23:19:50.171: E/AndroidRuntime(869):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.Activity.setContentView(Activity.java:1881)
02-07 23:19:50.171: E/AndroidRuntime(869):  at com.example.quizapp.MainActivity.onCreate(MainActivity.java:23)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.Activity.performCreate(Activity.java:5104)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-07 23:19:50.171: E/AndroidRuntime(869):  ... 11 more
02-07 23:19:50.171: E/AndroidRuntime(869): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.quiz.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.Fragment.instantiate(Fragment.java:592)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.Fragment.instantiate(Fragment.java:560)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.Activity.onCreateView(Activity.java:4709)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
02-07 23:19:50.171: E/AndroidRuntime(869):  ... 21 more
02-07 23:19:50.171: E/AndroidRuntime(869): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.quiz.Fragment1" on path: /data/app/com.example.quizapp-2.apk
02-07 23:19:50.171: E/AndroidRuntime(869):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
02-07 23:19:50.171: E/AndroidRuntime(869):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-07 23:19:50.171: E/AndroidRuntime(869):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-07 23:19:50.171: E/AndroidRuntime(869):  at android.app.Fragment.instantiate(Fragment.java:582)
02-07 23:19:50.171: E/AndroidRuntime(869):  ... 24 more

5 个答案:

答案 0 :(得分:1)

这似乎是违规行;

ft.replace(R.layout.main, newFragment1);

'replace'的第一个参数必须是' id '而不是'layout'。 'id'必须是View的id。

在不知道layout-xml(main.xml)的样子的情况下,我无法告诉你'replace'的第一个参数的正确值是什么。以下是您的代码的外观:

ft.replace(R.id.some_view_id, newFragment1);

您对此声明有同样的问题:

getFragmentManager().findFragmentById(R.layout.fragment1);

再次,R.layout ....错了。它应该是R. id ....

如果您可以向我们展示您的main.xml,我们可以帮助您更好。

显示XML后更新:

在onCreate中删除此声明:

ft = getFragmentManager().beginTransaction();

然后替换此

newFragment1 = (Fragment1) getFragmentManager().findFragmentById(R.layout.fragment1);

newFragment1 = (Fragment1) getFragmentManager().findFragmentById(R.id.fragment1);

替换此

ft.replace(R.layout.main, newFragment1);

ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, newFragment1).commit();

这可能会使您的代码崩溃,但您将自己替换R.id.fragment1 ......没有变化。你想要替换旧的newFragment1的 new 片段在哪里(因为你在onCreate中找到了一个)?

答案 1 :(得分:0)

为其提供

标签
ft.replace(R.layout.main, newFragment1, "my_new_awesome_frag");

这对我来说已经解决了。

答案 2 :(得分:0)

你还没有夸大你的片段。阅读findFragmentById()

的文档

明确指出

<强>返回 * 如果找到该片段,否则为null。 *

答案 3 :(得分:0)

此代码存在一些问题。第一个问题是你不能用“动态”片段替换“静态”片段。参见:

http://portabledroid.wordpress.com/2011/04/19/programmatic-and-layout-fragments/#comments

第二个问题是您需要提交您的交易。我建议在onClick处理程序中获取事务,执行替换并提交它。这里有一些示例代码,展示如何正确添加然后替换片段:

https://github.com/marakana/yamba/tree/yambaII

答案 4 :(得分:-1)

这是我的Fragment1.java类:

package com.example.quizapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment1, container, false);


    }
}