我试图从自定义视图中获取AlertDialog.我想我知道问题是什么,但我不知道如何解决它.我在LongClick上有一个(View V),而Button1正在使用"alertdialog"来查找数据.我在线上获得NullPointer异常TextView stax1 =(TextView)findViewById(R.id.xxx); 唯一有意义的是我让Dialog在查看"alerthelp"时查看它来自的活动.我该如何解决?
lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v)
{
LayoutInflater myLayout = LayoutInflater.from(context);
View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.setView(dialogView);
alertDialog.show();
Button button1 = (Button)dialogView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView stax1=(TextView)findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();
double sx1 = Double.parseDouble(sax1);
TextView textviewlay1 =(TextView)findViewById(R.id.m1ab);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
TextView textviewp1 = (TextView)findViewById(R.id.inputPrice);
String stringp1 = textviewp1.getText().toString();
Double intp1 = Double.parseDouble(stringp1);
double resultl1 …Run Code Online (Sandbox Code Playgroud) android nullpointerexception layout-inflater android-alertdialog
我是android编程的初学者.我正在使用按钮onclick使用以下代码将新页面添加到viewpager.但是,当在滑动活动上实例化项目时,viewpager返回错误的位置值.
向右滑动即可
正确位置:1,2,3(第2,3和4页)
改错位置:2,3,4
向左滑动即可
正确位置:3,2,1(第4页,第3页和第2页)
改为错误的位置:2,1,0
我想从instantiateItem()获取位置值,以从"views"数组列表中的特定位置获取视图,以显示在viewpager上.错误的位置会导致显示错误的视图.希望可以有人帮帮我!!!提前致谢.
添加按钮:
findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.image_text_1, null);
// the array list
views.add(itemView);
pagerAdapter.notifyDataSetChanged();
}
});
Run Code Online (Sandbox Code Playgroud)
ViewPager:
private class SlideAdapter extends PagerAdapter {
ArrayList<View> views = new ArrayList<View>();
@Override
public int getItemPosition (Object object)
{
int index = views.indexOf((View)object);
if(views.contains((View)object)){
return index;
}else{
return POSITION_NONE;
}
}
@Override
public Object instantiateItem (ViewGroup container, final int position)
{
//for example, when …Run Code Online (Sandbox Code Playgroud) 我正在构建一个导航片段。有一个按钮始终存在(主按钮),其他按钮则动态添加和删除。
我有一个 xml 资源文件,我从中填充一个按钮,更改它的图标并将其添加到片段根布局中。
除了按钮大小之外,几乎所有功能都有效。之后的按钮大小为 0 (getWidth()/getHeight())。
按钮导航.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/navigation_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/icon_test"
android:backgroundTint="@drawable/selector_bar_button"></Button>
Run Code Online (Sandbox Code Playgroud)
膨胀代码
Button butt = (Button) inflater.inflate(R.layout.button_navigation, null, false);
buttons.add(butt);
root_view.addView(butt);
Run Code Online (Sandbox Code Playgroud)
创建充气机
public void setParent(MainActivity activity){
parent = activity;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Run Code Online (Sandbox Code Playgroud)
变量
MainActivity parent;
LayoutInflater inflater;
int index;
ArrayList<Button> buttons;
Button button_main;
LinearLayout root_view;
Run Code Online (Sandbox Code Playgroud)
主按钮,具有与button_navigation.xml 完全相同的代码,并出现在片段的布局文件中。主按钮的大小很好。
我有一个约束布局,其中有 2 个主要元素:一个嵌套滚动视图,其中包含另一个布局文件中定义的元素列表和用作页脚的文本视图。这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="@+id/menu_scrollview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".sections.main.menu.MenuFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/menu_title"
style="@style/AppTheme.TextTitle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/mid_small_margin"
android:text="@string/menu_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include layout="@layout/list_item_menu_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<TextView
android:id="@+id/menu_title_2"
style="@style/AppTheme.TextTitle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/mid_small_margin"
android:text="@string/menu_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
这是list_item_menu_fragment.xml其中定义的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_elements_list"
style="@style/RectangularTextViewMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/mid_small_margin"
android:layout_marginTop="20dp"
android:layout_marginBottom="32dp"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/menu_item_profile"
app:layout_constraintBottom_toBottomOf="@+id/links_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/charge_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" …Run Code Online (Sandbox Code Playgroud) 我有一个相对较大的XML布局(38 kB,600行),其层次结构如下:
<ScrollView>
<LinearLayout>
<TabHost>
<LinearLayout>
<FrameLayout> //tab widget
<LinearLayout> //tab content
<LinearLayout> //section
<TextView> //section name
<LinearLayout orientation="horizontal"> //item 1 box
<TextView> //item 1 title
<Spinner> //item 1 picker
</LinearLayout>
<LinearLayout> //item 2 box
<TextView> //item 2 title
<Spinner> //item 2 picker
</LinearLayout>
... //18 other items
</LinearLayout>
... //4 other sections with 15 items each
</>
Run Code Online (Sandbox Code Playgroud)
它是一个数据输入表单,必须包含许多项目,我现在可以做的最好的事情是setContentView使用"正在加载..."对话框将数据包装和加载到AsyncTask中的微调器.
广泛使用主题是否会减缓观点通胀?视图inflater不需要查看加载的theme.xml,但是如果我将主题内联到布局xml中,它也会大大增加XML的大小,从而减慢解析器的速度.
我可以做些什么来简化布局,使其加载速度至少快两倍?我想我可能会试图摆脱水平的LinearLayouts,并使用TableLayout构建"部分".
嗨,我正在尝试在膨胀的视图中更改textview但应用程序崩溃.
我想要做的是用变量HomeTeam和AwayTeam中的内容填充文本视图这些变量不为null,因为当我记录它们时,那里有名字.
所以我的问题是如何更改文本视图并替换其内容
这是我尝试过的
for(int t = 0; t < 1; t++){
resultsDict = jArray.getJSONObject(t);
String HomeTeam = resultsDict.getString("hometeam");
String AwayTeam = resultsDict.getString("awayteam");
Log.v("lc","hometeam" + HomeTeam);
Log.v("lc","awayteam" + AwayTeam);
}
resultsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.resultscell,
null);
TextView homeTeam = (TextView) findViewById(R.id.HomeTeam);
homeTeam.setText(HomeTeam);
Run Code Online (Sandbox Code Playgroud)
膨胀的观点
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="20dp" >
<TextView
android:id="@+id/TextView04"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="-"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/blue"
android:textStyle="bold"
/>
<TextView
android:id="@+id/HomeScore"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/TextView04"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/blue"
android:textStyle="bold"
/>
<TextView
android:id="@+id/AwayScore"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/TextView04"
android:text="0" …Run Code Online (Sandbox Code Playgroud) 我在我的模拟器和设备上不一致地获得Inflate异常,但有时它工作正常
它显示在x16行-16,我有问题因为我正在使用两个图像一个在另一个之上
我有一个位图图像,并且我保留了.png图像.
这是我在xml中拥有的图像的原因吗?我的xml中有近10个图像
我在具有800交叉480分辨率的模拟器中运行此应用程序
我不知道如何解决这个问题,任何帮助总是受到赞赏.
Logcat显示这些消息
10-09 09:39:34.667: E/AndroidRuntime(339): FATAL EXCEPTION: main
10-09 09:39:34.667: E/AndroidRuntime(339): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.msat.carhome.clusters/co.msat.carhome.clusters.ClustersActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
10-09 09:39:34.667: E/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-09 09:39:34.667: E/AndroidRuntime(339): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-09 09:39:34.667: E/AndroidRuntime(339): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-09 09:39:34.667: E/AndroidRuntime(339): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-09 09:39:34.667: E/AndroidRuntime(339): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:39:34.667: E/AndroidRuntime(339): at android.os.Looper.loop(Looper.java:123)
10-09 09:39:34.667: E/AndroidRuntime(339): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-09 09:39:34.667: E/AndroidRuntime(339): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:39:34.667: E/AndroidRuntime(339): at java.lang.reflect.Method.invoke(Method.java:507)
10-09 09:39:34.667: …Run Code Online (Sandbox Code Playgroud) 我最近在设置一个包含3个片段的viewpager时遇到了一个问题.当应用程序运行时,它崩溃了
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
Run Code Online (Sandbox Code Playgroud)
以下是用于创建要传递给pageAdapter的片段列表列表的代码.
private Vector<Fragment> getFragments() {
Vector<Fragment> list = new Vector<Fragment>();
list.add(new Fragment1());
list.add(new Fragment2());
list.add(new Fragment3());
return list;
Run Code Online (Sandbox Code Playgroud)
除了使用不同布局创建的片段之外,每个片段基本相同.这是我对其中一个片段的原始代码.公共类Fragment1扩展Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.fragment1, container);
return v;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我像这样运行它时,它一直与IllegalStateException崩溃.我发现问题来自于正在创建的片段.经过一些谷歌搜索后,我尝试将片段的代码更改为此.
公共类Fragment1扩展Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.fragment1, container, false);
return v;
}
} …Run Code Online (Sandbox Code Playgroud) java android illegalstateexception android-fragments layout-inflater
大家好,我想FieldSet在 Android 中创建一个组件。(类似html),所以像那个。所以我希望我的自定义组件有孩子并有一个板。电路板部分已经完成,但我在显示组件时遇到了一些问题。我正在关注这个答案。这是我的部分:
字段集.java :
public class FieldSet extends ViewGroup {
//... 3 constructors
@Override
protected void onFinishInflate()
int index = getChildCount();
// Collect children declared in XML.
View[] children = new View[index];
while(--index >= 0) {
children[index] = getChildAt(index);
}
// Pressumably, wipe out existing content (still holding reference to it).
this.detachAllViewsFromParent();
// Inflate new "template".
final View template = LayoutInflater.from(getContext()).inflate(R.layout.field_set, this, true);
// Obtain reference to a new container within …Run Code Online (Sandbox Code Playgroud) android custom-component android-custom-view layout-inflater