我按照http://www.techotopia.com/index.php/Using_Fragments_in_Android_-_A_Worked_Example上的教程创建了一个应用, 但是我有一个错误。
logcat的:
05-02 08:16:22.044: D/dalvikvm(1846): Late-enabling CheckJNI
05-02 08:16:22.080: D/AndroidRuntime(1846): Shutting down VM
05-02 08:16:22.080: W/dalvikvm(1846): threadid=1: thread exiting with uncaught exception (group=0xa4d81b20)
05-02 08:16:22.096: E/AndroidRuntime(1846): FATAL EXCEPTION: main
05-02 08:16:22.096: E/AndroidRuntime(1846): Process: com.example.myfragmentexample, PID: 1846
05-02 08:16:22.096: E/AndroidRuntime(1846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfragmentexample/com.example.myfragmentexample.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.os.Looper.loop(Looper.java:136)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.reflect.Method.invoke(Method.java:515)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 08:16:22.096: E/AndroidRuntime(1846): at dalvik.system.NativeStart.main(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.setContentView(Activity.java:1929)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.example.myfragmentexample.MainActivity.onCreate(MainActivity.java:12)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.performCreate(Activity.java:5231)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 11 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragmentexample.ToolbarFragment: make sure class name exists, is public, and has an empty constructor that is public
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:597)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:561)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.onCreateView(Activity.java:4778)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 21 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.fragmentexample.ToolbarFragment" on path: DexPathList[[zip file "/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1, /system/lib]]
05-02 08:16:22.096: E/AndroidRuntime(1846): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:583)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 24 more
MainActivity.java:
package com.example.myfragmentexample;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements
ToolbarFragment.ToolbarListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtononClick(int fontsize, String text) {
TextFragment textfragment = (TextFragment) getSupportFragmentManager()
.findFragmentById(R.id.text_fragment);
textfragment.zmienWlasciwosci(fontsize, text);
}
}
activity_main.xml中:
<RelativeLayout 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/toolbar_fragment"
android:name="com.example.fragmentexample.ToolbarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="@layout/toolbar_fragment" />
<fragment
android:id="@+id/text_fragment"
android:name="com.example.fragmentexample.TextFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
tools:layout="@layout/text_fragment" />
</RelativeLayout>
ToolbarFragment.java:
package com.example.myfragmentexample;
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.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class ToolbarFragment extends Fragment implements
OnSeekBarChangeListener {
private static int seekvalue = 10;
private static EditText edittext;
ToolbarListener activityCallback;
public interface ToolbarListener {
public void onButtononClick(int position, String text);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCallback = (ToolbarListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ToolbarListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.toolbar_fragment, container,
false);
edittext = (EditText) view.findViewById(R.id.editText);
SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
public void buttonClicked(View view) {
activityCallback.onButtononClick(seekvalue, edittext.getText()
.toString());
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
seekvalue = arg1;
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
toolbar_fragment.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" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText"
android:layout_marginTop="14dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:layout_below="@+id/seekBar"
android:text="@string/button_text" />
</RelativeLayout>
TextFragment.java:
package com.example.myfragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TextFragment extends Fragment {
private static TextView textview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.text_fragment, container, false);
textview = (TextView) view.findViewById(R.id.textView1);
return view;
}
public void zmienWlasciwosci (int fontsize, String text){
textview.setTextSize(fontsize);
textview.setText(text);
}
}
text_fragment.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/text_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfragmentexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfragmentexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我阅读了很多有关此错误的帖子,但我的代码中找不到任何错误。我延长了android.support.v4.app.FragmentActivity
。
答案 0 :(得分:8)
引起:java.lang.ClassNotFoundException:没有找到类 路径上的“com.example.fragmentexample.ToolbarFragment”: DexPathList [[zip文件 “/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1, /系统/ lib中]]
此
android:name="com.example.fragmentexample.ToolbarFragment"
必须
android:name="com.example.myfragmentexample.ToolbarFragment"
导致ToolbarFragment.java
的包名称为
package com.example.myfragmentexample;
同样适用于TextFragment
android:name="com.example.myfragmentexample.TextFragment"
答案 1 :(得分:1)
尝试在正确的位置添加正确的布局,例如ex。在我的情况下,我将 colors.xml 放在 values-w820p 中,然后它给了classtype的运行时异常。我将文件夹从此更改为简单值... taddda !!有效! :)
答案 2 :(得分:0)
我也是Android新手,今天遇到了同样的错误。我找不到适合我的解决方案,但有一件事我很清楚,当android无法解析布局xml中某个元素的强制属性时会出现此问题。
所以我彻底检查了我的布局,发现我错误地创建了一个ID
<item type="id" name="login"/>
在ids.xml中。此ID与属性
冲突 android:imeActionId="@+id/login"
。
希望这有助于某人。
答案 3 :(得分:0)
就我而言,Google地图未正确初始化。在日志控制台中会显示一条有关此消息的消息。我必须添加
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
到AndroidManifest中的<application>
。