当前位置: 首页 > 知识库问答 >
问题:

错误:不兼容类型:NewSubscription无法转换为上下文

祁嘉言
2023-03-14

我试图创建一个动态表单布局,当我构建我的项目时,我在代码的第37行得到以下错误“错误:不兼容的类型:NewSubscription不能转换为上下文”。是不是因为这是片段,不是Java课?如何解决此错误?

NewSubscription.java

package com.example.activitymanagement;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;



public class NewSubscription extends Fragment {

    private LinearLayout mainLinear;
    private Button createEdittext;
    private int edittextcount =1;
    private EditText editTextbox;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments
        return inflater.inflate(R.layout.fragment_new_subscription, container, false);

        mainLinear = (LinearLayout) getView().findViewById(R.id.HolderLayout);
        createEdittext = (Button) getView().findViewById(R.id.CreateEdittext);
        createEdittext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Starting a new Intent
                EditText editTextbox = new EditText(NewSubscription.this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                // param.setMargins(int left,int top , int right,int bottom)
                params.setMargins(20, 10, 20, 10);
                //  params.weight = 1.0f;
                params.gravity = Gravity.CENTER_HORIZONTAL;  /// this is layout gravity of textview
                editTextbox.setLayoutParams(params);
                editTextbox.setBackgroundColor(Color.parseColor("#3F51B5"));
                editTextbox.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
                editTextbox.setTextColor(Color.parseColor("#ffffff"));
                editTextbox.setTypeface(null, Typeface.BOLD);
                editTextbox.setTextSize(18);
                editTextbox.setHint("Edittext "+ edittextcount);
                editTextbox.setMinimumWidth(140);
                edittextcount = edittextcount+1;
                mainLinear.addView(editTextbox);


            }
        });
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("Menu 1");
    }

}

fragment_new_subscription.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"
    android:columnCount="2"
    android:id="@+id/HolderLayout"
    >

    <!-- Company Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Company Name"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="60dip"
        android:textSize="17dip"/>

    <!-- Input Company Name -->
    <EditText android:id="@+id/inputName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dip"
        android:layout_marginBottom="15dip"
        android:singleLine="true"/>

       <!-- Description Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Resources to manage"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="10dip"
        android:textSize="17dip"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/CreateEdittext"
        android:text="Create fields"
        android:layout_margin="5dp"
        android:textColor="@color/colorPrimary"/>

    <!-- Button Create Company -->
    <Button android:id="@+id/btnCreateProduct"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Create Company"/>

</LinearLayout>

共有2个答案

司徒英卓
2023-03-14

替换newsubscription.this在第37行中使用getActivity(),如下所示:

EditText editTextbox = new EditText(getActivity());
商曦
2023-03-14

无法转换为上下文

对于片段-->GetActivity()

对于activity---->活动。这

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_new_subscription, container, false);
        mainLinear = (LinearLayout) rootView.findViewById(R.id.HolderLayout);
        createEdittext = (Button) rootView.findViewById(R.id.CreateEdittext);
         .......
        return rootView;
    }

然后

 EditText editTextbox = new EditText(getActivity());

getActivity()通常在片段中使用,以获取插入或膨胀它们的activity的上下文。

 类似资料: