我需要一些关于ActionBar Sherlock片段的帮助。
我对Android编程很新,但我基本上跟着http://goo.gl/Nikyg(stackoverflow post)来创建我的片段,但每次我尝试在android 2.2或4.0上运行时应用程序崩溃
任何人都可以帮助我吗?代码如下:
Mainactivity.java
package br.com.example.fragmented;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class MainActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
布局文件activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<fragment class="br.com.example.fragment1"
android:id="@+id/fragment1"
android:name="com.actionbarsherlock.app.SherlockFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.85" />
</LinearLayout>
fragment1.java
package br.com.example.fragmented;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
public class fragment1 extends SherlockFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("First Fragment", "onCreate()");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("First Fragment", "onCreateView()");
return inflater.inflate(R.layout.fragment1layout, container, false);
}
}
fragment1layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#000000">
<Button android:text="Landscape"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
任何人都可以指出我错过了错误吗? OH和我已经将主题设置为Sherlock。顺便提一下Manifest。
添加日志:
07-26 20:22:14.297: E/AndroidRuntime(401): FATAL EXCEPTION: main
07-26 20:22:14.297: E/AndroidRuntime(401): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.example.fragmented/br.com.example.fragmented.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
07-26 20:22:14.297: E/AndroidRuntime(401): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.os.Looper.loop(Looper.java:123)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-26 20:22:14.297: E/AndroidRuntime(401): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 20:22:14.297: E/AndroidRuntime(401): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 20:22:14.297: E/AndroidRuntime(401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-26 20:22:14.297: E/AndroidRuntime(401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-26 20:22:14.297: E/AndroidRuntime(401): at dalvik.system.NativeStart.main(Native Method)
07-26 20:22:14.297: E/AndroidRuntime(401): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
07-26 20:22:14.297: E/AndroidRuntime(401): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-26 20:22:14.297: E/AndroidRuntime(401): at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:857)
07-26 20:22:14.297: E/AndroidRuntime(401): at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:251)
07-26 20:22:14.297: E/AndroidRuntime(401): at br.com.example.fragmented.MainActivity.onCreate(MainActivity.java:12)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-26 20:22:14.297: E/AndroidRuntime(401): ... 11 more
07-26 20:22:14.297: E/AndroidRuntime(401): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment br.com.example.fragment1: make sure class name exists, is public, and has an empty constructor that is public
07-26 20:22:14.297: E/AndroidRuntime(401): at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
07-26 20:22:14.297: E/AndroidRuntime(401): ... 20 more
07-26 20:22:14.297: E/AndroidRuntime(401): Caused by: java.lang.ClassNotFoundException: br.com.example.fragment1 in loader dalvik.system.PathClassLoader[/data/app/br.com.example.fragmented-1.apk]
07-26 20:22:14.297: E/AndroidRuntime(401): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-26 20:22:14.297: E/AndroidRuntime(401): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-26 20:22:14.297: E/AndroidRuntime(401): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-26 20:22:14.297: E/AndroidRuntime(401): at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
07-26 20:22:14.297: E/AndroidRuntime(401): ... 23 more
答案 0 :(得分:1)
删除xml中的class属性,并使用您的片段名称作为name属性,而不是ABS。