当我单击开始按钮时,我希望我的活动切换到片段。当我尝试替换时,它给了我一个空指针异常。这是为什么?
我需要一些帮助,因为我很快就会交出这个任务。
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