我在尝试开发一个fragemnt和片段活动简单的例子时遇到了问题。 这是我的项目代码
Mainactivity.java
package com.example.fragment_test;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.example.fragment_test.MainFragment.UiListener;
public class MainActivity extends FragmentActivity implements UiListener{
private MainFragment fragment;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
fragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.main_fragment);
}
public void onButtonClicked(){
// handle button clicked
}
}
mainfragment.java
package com.example.fragment_test;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
public interface UiListener{
public void onButtonClicked();
}
private UiListener uiCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
uiCallback = (UiListener) activity; // check if the interface is implemented
}catch(ClassCastException e){
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
uiCallback.onButtonClicked();
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment android:name="MainFragment"
android:id="@+id/main_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linProgress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
logcat的
06-14 05:06:49.771: I/Process(1563): Sending signal. PID: 1563 SIG: 9
06-14 05:06:50.761: D/AndroidRuntime(1617): Shutting down VM
06-14 05:06:50.761: W/dalvikvm(1617): threadid=1: thread exiting with uncaught exception (group=0xb4b02ba8)
06-14 05:06:50.791: E/AndroidRuntime(1617): FATAL EXCEPTION: main
06-14 05:06:50.791: E/AndroidRuntime(1617): Process: com.example.fragment_test, PID: 1617
06-14 05:06:50.791: E/AndroidRuntime(1617): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragment_test/com.example.fragment_test.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.os.Looper.loop(Looper.java:136)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.reflect.Method.invoke(Method.java:515)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-14 05:06:50.791: E/AndroidRuntime(1617): at dalvik.system.NativeStart.main(Native Method)
06-14 05:06:50.791: E/AndroidRuntime(1617): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Activity.setContentView(Activity.java:1929)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.example.fragment_test.MainActivity.onCreate(MainActivity.java:13)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Activity.performCreate(Activity.java:5231)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-14 05:06:50.791: E/AndroidRuntime(1617): ... 11 more
06-14 05:06:50.791: E/AndroidRuntime(1617): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment MainFragment: make sure class name exists, is public, and has an empty constructor that is public
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Fragment.instantiate(Fragment.java:597)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Fragment.instantiate(Fragment.java:561)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Activity.onCreateView(Activity.java:4778)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-14 05:06:50.791: E/AndroidRuntime(1617): ... 21 more
06-14 05:06:50.791: E/AndroidRuntime(1617): Caused by: java.lang.ClassNotFoundException: Didn't find class "MainFragment" on path: DexPathList[[zip file "/data/app/com.example.fragment_test-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragment_test-1, /system/lib]]
06-14 05:06:50.791: E/AndroidRuntime(1617): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Fragment.instantiate(Fragment.java:583)
06-14 05:06:50.791: E/AndroidRuntime(1617): ... 24 more
06-14 05:06:53.331: I/Process(1617): Sending signal. PID: 1617 SIG: 9
我尝试了几个提出的解决方案但没有任何成功。你能不能帮我运行这个例子
答案 0 :(得分:1)
请在xml文件中提供类的完全限定名称
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/main_fragment"
android:name="com.example.fragment_test.MainFragment" // UPDATE HERE
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>