0

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

我需要一些帮助,因为我很快就会交出这个任务。

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

}

这是日志猫:

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
4

5 回答 5

1

看来这是有问题的路线;

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

'replace' 的第一个参数必须是 ' id ' 而不是 'layout'。'id' 必须是视图的 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的新片段在哪里(因为您在 onCreate 中找到了那个片段)?

于 2013-02-07T22:06:45.287 回答
0

为它提供一个标签

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

这在过去对我来说已经解决了。

于 2013-02-07T21:04:52.367 回答
0

这段代码有几个问题。第一个问题是您不能用“动态”片段替换“静态”片段。看:

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

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

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

于 2013-02-08T00:06:37.363 回答
0

you have not inflated your fragment . Read the documentation of findFragmentById()

It clearly states

Returns *The fragment if found or null otherwise.*

于 2013-02-07T21:06:03.387 回答
-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);


    }
}
于 2013-02-08T08:02:43.243 回答