gpt4 book ai didi

java - Android fragment 演示错误

转载 作者:行者123 更新时间:2023-12-01 23:03:19 24 4
gpt4 key购买 nike

我正在研究 Wei-Meng Lee 的开始 Android 4 应用程序开发,并停留在第 2 章,他们介绍了 fragment 。我已经严格按照演示代码进行操作,但应用程序在运行时仍然失败。我在互联网上找到的任何解决方法总是会产生相同的错误。查看 LogCat 发现主要来源是 java.lang.ClassNotFoundExceptionandroid.app.Fragment$InstantiationException

我尝试使用 android.support.v4.app.Fragment 以及 android.support.v4.app.FragmentActivity 并有我的主要相反, Activity 扩展了它,但两者都没有提供解决方案。我检查了构建路径并尝试了一会儿,但也没有运气。我不知道为什么程序找不到我的类,因为它们位于我认为正确的目录中。

这是 LogCat 错误输出:

04-20 14:10:35.572: E/AndroidRuntime(1114): FATAL EXCEPTION: main
04-20 14:10:35.572: E/AndroidRuntime(1114): Process: solaris.data.demofragment, PID: 1114
04-20 14:10:35.572: E/AndroidRuntime(1114): java.lang.RuntimeException: Unable to start activity ComponentInfo{solaris.data.demofragment/solaris.data.demofragment.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.os.Looper.loop(Looper.java:136)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 14:10:35.572: E/AndroidRuntime(1114): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 14:10:35.572: E/AndroidRuntime(1114): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 14:10:35.572: E/AndroidRuntime(1114): at dalvik.system.NativeStart.main(Native Method)
04-20 14:10:35.572: E/AndroidRuntime(1114): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-20 14:10:35.572: E/AndroidRuntime(1114): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Activity.setContentView(Activity.java:1929)
04-20 14:10:35.572: E/AndroidRuntime(1114): at solaris.data.demofragment.MainActivity.onCreate(MainActivity.java:19)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Activity.performCreate(Activity.java:5231)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-20 14:10:35.572: E/AndroidRuntime(1114): ... 11 more
04-20 14:10:35.572: E/AndroidRuntime(1114): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment solaris.data.demofragment.fragment1: make sure class name exists, is public, and has an empty constructor that is public
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Fragment.instantiate(Fragment.java:597)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Fragment.instantiate(Fragment.java:561)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Activity.onCreateView(Activity.java:4778)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-20 14:10:35.572: E/AndroidRuntime(1114): ... 21 more
04-20 14:10:35.572: E/AndroidRuntime(1114): Caused by: java.lang.ClassNotFoundException: Didn't find class "solaris.data.demofragment.fragment1" on path: DexPathList[[zip file "/data/app/solaris.data.demofragment-1.apk"],nativeLibraryDirectories=[/data/app-lib/solaris.data.demofragment-1, /system/lib]]
04-20 14:10:35.572: E/AndroidRuntime(1114): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Fragment.instantiate(Fragment.java:583)
04-20 14:10:35.572: E/AndroidRuntime(1114): ... 24 more

主要 Activity :

package solaris.data.demofragment;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}

activity_main.xml

<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="horizontal" >

<fragment
android:name="solaris.data.demofragment.fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<fragment
android:name="solaris.data.demofragment.fragment2"
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#00FF00"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>

Fragment1.java

package solaris.data.demofragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}

Fragment2.java 和fragment2.xml 与其各自对应的1 版本相同,除了名称、显示的文本和颜色之外。所有 .xml 文件都位于 res/layout 文件夹中,所有 java 文件都位于 src/solaris.data.demofragment 包中。最小 sdk 版本为 14,目标为 19。

最佳答案

您在 activity_main.xml 中定义的 fragment 的 name 属性不正确。

而不是

android:name="solaris.data.demofragment.fragment1"

应该是

android:name="solaris.data.demofragment.Fragment1"

这是因为它们区分大小写,正如您所定义的那样

public class Fragment1 extends Fragment

因此,当 android 膨胀您的 activity_main.xml 时,它无法找到名为 fragment1 的 fragment 。如果类是 Fragment2,这也适用于您的 fragment2

关于java - Android fragment 演示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23186164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com