gpt4 book ai didi

java - Android 中 Fragment Activity 出错

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:57 26 4
gpt4 key购买 nike

我正在按照教程尝试 fragment 应用程序并收到以下错误:

06-28 04:21:47.118: D/AndroidRuntime(1491): Shutting down VM
06-28 04:21:47.128: W/dalvikvm(1491): threadid=1: thread exiting with uncaught exception (group=0xb4af7b90)
06-28 04:21:47.158: E/AndroidRuntime(1491): FATAL EXCEPTION: main
06-28 04:21:47.158: E/AndroidRuntime(1491): Process: com.example.fragmentapp, PID: 1491
06-28 04:21:47.158: E/AndroidRuntime(1491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentapp/com.example.fragmentapp.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Looper.loop(Looper.java:137)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.NativeStart.main(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.setContentView(Activity.java:1928)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.example.fragmentapp.MainActivity.onCreate(MainActivity.java:13)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.performCreate(Activity.java:5243)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 11 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.pavan.fragmentdemo.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:597)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:561)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.onCreateView(Activity.java:4777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 21 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pavan.fragmentdemo.MyListFragment" on path: DexPathList[[zip file "/data/app/com.example.fragmentapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragmentapp-2, /system/lib]]
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:583)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 24 more

代码如下:主Activity.java

package com.example.fragmentapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity implements
ListFragment.Communicator {

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

@Override
public void Message(String OS_Name) {
DetailFragment detailfragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail_Fragment);
if (detailfragment != null && detailfragment.isInLayout()) {
detailfragment.setText(OS_Name);
}
else {
Intent intent = new Intent(getApplicationContext(),
DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(DetailActivity.os_name, OS_Name);
intent.putExtras(extras);
startActivity(intent);
}
}
}

DetailActivity.java代码

package com.example.fragmentapp;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class DetailActivity extends Activity {

public static String os_name = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.detail_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString(os_name);
DetailFragment detailFragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
detailFragment.setText(name);
}
}
}

DetailFragment.java代码

package com.example.fragmentapp;

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

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

// we call this method when button from listfragment is clicked
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.display_tv);
view.setText(item);
}
}

ListFragment.java代码

package com.example.fragmentapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ListFragment extends Fragment implements OnClickListener {

private Communicator communicator;

Button android_btn, ios_btn, windows_btn;

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof Communicator) {
communicator = (Communicator) activity;
}
else {
throw new
ClassCastException(activity.toString()
+ " must implemenet MyListFragment.Communicator");
}
}

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view =
inflater.inflate(R.layout.list_fragment, container, false);

// Initialize Views android_btn = (Button)
view.findViewById(R.id.android_btn_id);
ios_btn = (Button)
view.findViewById(R.id.ios_btn_id);
windows_btn = (Button)
view.findViewById(R.id.windows_btn_id);

// set on click Listeners for buttons
android_btn.setOnClickListener(this);
ios_btn.setOnClickListener(this);
windows_btn.setOnClickListener(this);

return view;
}

//Create Interface
public interface Communicator {
public void Message(String OS_Name);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.android_btn_id:
updateFragment("Android");
break;
case R.id.ios_btn_id:
updateFragment("IOS");
break;

case R.id.windows_btn_id:
updateFragment("Windows");
break;
}
}

private void updateFragment(String OS_Name) {
communicator.Message(OS_Name);
}
}

在布局文件夹中Activity_main.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="horizontal" >

<fragment
android:id="@+id/list_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>

<fragment
android:id="@+id/detail_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="2"
class="com.pavan.fragmentdemo.DetailFragment" >
</fragment>

</LinearLayout>

detail_fragment.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF99"
android:orientation="vertical"
android:padding="20dp" >

<TextView
android:id="@+id/display_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="40sp" />

</LinearLayout>

list_fragment.xml代码

<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFF99"
android:orientation="vertical"
android:padding="5dp" >

<Button
android:id="@+id/android_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />

<Button
android:id="@+id/ios_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IOS" />

<Button
android:id="@+id/windows_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Windows" />

</LinearLayout>

在布局端口中代码activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
android:id="@+id/list_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>

</LinearLayout>

detail_activity.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.pavan.fragmentdemo.DetailFragment" />

</LinearLayout>

最佳答案

您有不同的 fragment 名称:在 xml com.pavan.fragmentdemo.MyListFragment 中在 java com.example.fragmentapp.MyListFragment

修复包名称

关于java - Android 中 Fragment Activity 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097652/

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