我对 2.2 版中的片段的兼容性有疑问。我使用“Sherlock Action Bar”来创建操作栏,但我无法创建两个片段。我一直收到这个错误:
01-07 12:51:56.124: E/AndroidRuntime(475): FATAL EXCEPTION: main 01-07
12:51:56.124: E/AndroidRuntime(475): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.example.pruebasherlock_v4/com.example.pruebasherlock_v4.MainActivity}:
android.view.InflateException: Binary XML file line #7: Error
inflating class fragment 01-07 12:51:56.124: E/AndroidRuntime(475):
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.os.Handler.dispatchMessage(Handler.java:99) 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.os.Looper.loop(Looper.java:123) 01-07 12:51:56.124:
E/AndroidRuntime(475): at
android.app.ActivityThread.main(ActivityThread.java:4627) 01-07
12:51:56.124: E/AndroidRuntime(475): at
java.lang.reflect.Method.invokeNative(Native Method) 01-07
12:51:56.124: E/AndroidRuntime(475): at
java.lang.reflect.Method.invoke(Method.java:521) 01-07 12:51:56.124:
E/AndroidRuntime(475): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-07 12:51:56.124: E/AndroidRuntime(475): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-07
12:51:56.124: E/AndroidRuntime(475): at
dalvik.system.NativeStart.main(Native Method) 01-07 12:51:56.124:
E/AndroidRuntime(475): Caused by: android.view.InflateException:
Binary XML file line #7: Error inflating class fragment 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.inflate(LayoutInflater.java:407) 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.inflate(LayoutInflater.java:320) 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.inflate(LayoutInflater.java:276) 01-07
12:51:56.124: E/AndroidRuntime(475): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.app.Activity.setContentView(Activity.java:1647) 01-07
12:51:56.124: E/AndroidRuntime(475): at
com.example.pruebasherlock_v4.MainActivity.onCreate(MainActivity.java:12)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-07 12:51:56.124: E/AndroidRuntime(475): ... 11 more 01-07
12:51:56.124: E/AndroidRuntime(475): Caused by:
java.lang.ClassNotFoundException: android.view.fragment in loader
dalvik.system.PathClassLoader[/data/app/com.example.pruebasherlock_v4-1.apk]
01-07 12:51:56.124: E/AndroidRuntime(475): at
dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-07 12:51:56.124: E/AndroidRuntime(475): at
java.lang.ClassLoader.loadClass(ClassLoader.java:573) 01-07
12:51:56.124: E/AndroidRuntime(475): at
java.lang.ClassLoader.loadClass(ClassLoader.java:532) 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.createView(LayoutInflater.java:466) 01-07
12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
01-07 12:51:56.124: E/AndroidRuntime(475): at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-07 12:51:56.124: E/AndroidRuntime(475): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
01-07 12:51:56.124: E/AndroidRuntime(475): ... 20 more
我的 main_activity 包含以下内容:
package com.example.pruebasherlock_v4;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我的 main.xml 包含以下内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/listaIzq"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
class="com.example.pruebasherlock_v4.Lista"/>
<fragment
android:id="@+id/detallesDer"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="fill_parent"
class="com.example.pruebasherlock_v4.Detalle" />
</LinearLayout>
而“lista”和“Detalle”类包含以下内容:
清单:
package com.example.pruebasherlock_v4;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListFragment;
public class Lista extends SherlockListFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
//se sobreescribe este método como el que siempre "carga" la información a mostrar en el fragment
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
//al hacer tap en algun elemento de la vista, setea el texto del elemento en el nuevo fragment
public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Detalle fragment = (Detalle) getFragmentManager().findFragmentById(R.id.detallesDer);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(item);
} else {
/*Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
intent.putExtra("value", item);
startActivity(intent);*/
}
}
}
细节:
package com.example.pruebasherlock_v4;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
public class Detalle extends SherlockFragment {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detalle, container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.textoDetalle);
view.setText(item);
}
}
我觉得放了很多代码,但不是因为它是错误的。
非常感谢您的帮助。