姚望785 2021-06-05 16:46 采纳率: 50%
浏览 138

Android studio 写出来的app闪退报错

Android studio 写出来的app第一次为空,后来就闪退报错。写的内容是添加两个xml文件,分别是textview和imageview,分别添加对应的Java文件,java文件中写了onCreatView方法,在activity_main中写两个framelayout,两个framelayout里面分别引用textview和imageview的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">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />
</LinearLayout>



package com.example.fragmentdemo;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class TextViewFragment extends Fragment {
    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @org.jetbrains.annotations.NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        inflater.inflate(R.layout.fragment_textview/*类型 int resource*/,container/* 类型 ViewGroup*/,false);
        View view = null;
        return view;
    }
}




<?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">
<!--android:name="com.example.fragmentdemo.TextViewFragment" 当前的Fragment由哪一个类 对他的内容或者行为进行处理-->
   <FrameLayout
       android:name="com.example.fragmentdemo.ImageViewFragment"
       android:id="@+id/image_fragment"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

   </FrameLayout>
    <FrameLayout
        android:id="@+id/text_fragment"
        android:name="com.example.fragmentdemo.TextViewFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </FrameLayout>
</LinearLayout>

  • 写回答

1条回答 默认 最新

  • scenelyLiu 2024-02-21 16:32
    关注

    问题1

    img


    这个包倒错了,使用androidx下面的
    问题2
    onCreateView里面
    View view = inflater.inflate(R.layout.fragment textview,container,false)
    return view
    希望我的回答能解决您的问题,点下采纳,谢谢

    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机检测,可以是封装好的DLL,可付费
  • ¥15 kafka无法正常启动(只启动了一瞬间会然后挂了)
  • ¥15 开发一个类似百度网盘的软件,在主页 文件列表点击进入文件夹,在文件夹里面还有文件夹,代码该怎么写?
  • ¥30 使用matlab将观测点聚合成多条目标轨迹
  • ¥15 Workbench中材料库无法更新,如何解决?
  • ¥20 如何推断此服务器配置
  • ¥15 关于github的项目怎么在pycharm上面运行
  • ¥15 内存地址视频流转RTMP
  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 有没有整苹果智能分拣线上图像数据