我正在尝试创建动态表单布局,当我构建项目时,在代码的第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>
答案 0 :(得分:1)
无法转换为上下文
对于 FRAGMENT-> 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()通常用于片段中,以获取 它们插入或膨胀的活动。
答案 1 :(得分:0)
在第37行中使用NewSubscription.this
代替getActivity()
,如下所示:
EditText editTextbox = new EditText(getActivity());