错误:不兼容的类型:新订阅无法转换为上下文



>我正在尝试创建一个动态表单布局,当我构建项目时,在我的代码第 37 行收到以下错误"错误:不兼容的类型:NewSubscription 无法转换为上下文"。是因为这是一个片段而不是一个 java 类吗?如何解决此错误?

新订阅.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>

无法转换为上下文

对于 FRAGMENT-->getActivity((

对于活动--->活动.this

 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(( 通常用于片段中以获取 插入或充气的活动。

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

EditText editTextbox = new EditText(getActivity());

最新更新