gpt4 book ai didi

android - 为什么在尝试在 ScrollView 中单击按钮时设置 EditText 时会出现 Nullpointer 异常?

转载 作者:行者123 更新时间:2023-11-29 16:01:56 24 4
gpt4 key购买 nike

我有一个 ScrollView,它有一个固定的页眉和页脚。我在我的类(class)中实现了 onClickListener。单击按钮时,我试图在 EditText 中设置文本。但是当我这样做时,我得到了一个Nullpointer Exception

我的代码如下:

内容.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/editText04"
android:layout_width="190dp"
android:layout_height="23dp"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="@drawable/roundedittext"
android:ems="10"
android:paddingLeft="25dp"
android:text="Task 1"
android:textColor="#FFFFFF"
android:textSize="17dp" >

<requestFocus />
</EditText>
</RelativeLayout>

可滚动内容.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- Header aligned to top -->

<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:background="@android:color/white" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="18dp"
android:src="@drawable/propic" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/imageView1"
android:text="Jack Reacher"
android:textSize="17dp"
android:textColor="#000" />
</RelativeLayout>

<!-- Footer aligned to bottom -->

<RelativeLayout
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:visibility="visible"
android:background="@android:color/black" >

<Button
android:id="@+id/button1"
android:layout_width="160dp"
android:layout_height="50dp"
android:background="@color/green"
android:layout_alignParentTop="true"
android:text="Accept"
android:textSize="17dp"
android:visibility="visible"
android:textColor="#FFFFFF"/>

<Button
android:id="@+id/button2"
android:layout_width="160dp"
android:layout_height="50dp"
android:textSize="17dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:background="@color/red"
android:text="Decline"
android:visibility="visible"
android:textColor="#FFFFFF"/>

<Button
android:id="@+id/button3"
android:layout_width="112dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Completed"
android:visibility="gone"
android:textColor="@android:color/white"
android:background="@color/green"/>

<Button
android:id="@+id/button4"
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button3"
android:text="Ongoing"
android:visibility="gone"
android:textColor="@android:color/white"
android:background="@color/yellow" />

<Button
android:id="@+id/button5"
android:layout_width="105dp"
android:background="@color/red"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Decline"
android:visibility="gone"
android:textColor="@android:color/white" />

</RelativeLayout>
<!-- Scrollable Item below header and above footer -->
<ScrollView
android:id="@+id/scrollableContents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/footer"
android:background="@android:color/white"
android:layout_below="@id/header" >

<!-- Inflate the contents of the ScrollView dynamicaly -->

</ScrollView>

</RelativeLayout>

我的contactstemp.java

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// final Databasehandler2 db=new Databasehandler2(getActivity().getApplicationContext());
View view = inflater.inflate(R.layout.scrollable_contents, container,
false);
ScrollView scrollable_contents = (ScrollView)view.findViewById(R.id.scrollableContents);
View v2 =getActivity().getLayoutInflater().inflate(R.layout.contents, scrollable_contents);
EditText stat=(EditText)v2.findViewById(R.id.editText04);
scrollable_contents.addView(v2);
return view;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
Toast.makeText(getActivity(),"accept clicked",1000).show();

stat.setText("Paused");
/*b1.setVisibility(v.VISIBLE);
b2.setVisibility(v.VISIBLE);
b3.setVisibility(v.VISIBLE);*/
break;
}
}

堆栈跟踪:

    E/AndroidRuntime(5223): FATAL EXCEPTION: main
E/AndroidRuntime(5223): java.lang.NullPointerException
E/AndroidRuntime(5223): at com.example.taskmanager.mycontactstemp.onClick(mycontactstemp.java:106)
E/AndroidRuntime(5223): at android.view.View.performClick(View.java:4204)
E/AndroidRuntime(5223): at android.view.View$PerformClick.run(View.java:17355)
E/AndroidRuntime(5223): at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(5223): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(5223): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(5223): at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(5223): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(5223): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(5223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(5223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(5223): at dalvik.system.NativeStart.main(Native Method)

最佳答案

这个stat是空的

EditText stat=(EditText)view.findViewById(R.id.EditText04)

您膨胀的布局是scrollable_contents.xml

View view = inflater.inflate(R.layout.scrollable_contents, container,
false);

并且您使用相同的 View 对象来初始化 EditText

它属于contents.xml。此外,id 是 editText04 而不是 EditText04

并且您正在为 null 的 stat 调用 setText

stat.setText("Paused");

修复 Inflate 布局

View v= getActivity().getLayoutInflater().inflate(R.layout.contents, scrollable_contents);
stat=(EditText)v.findViewById(R.id.editText04);

最后将这个 View 添加到ScrollView

scrollable_contents.addView(v);

编辑:

改变这个

 EditText stat=(EditText)v2.findViewById(R.id.editText04);

 stat=(EditText)v2.findViewById(R.id.editText04);

编辑 2:

我不知道为什么这会被否决

scrollable_contents.xml改为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- Header aligned to top -->

<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:background="@android:color/white" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:background="@drawable/ic_launcher"
android:layout_marginLeft="18dp" />

<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:text="Jack Reacher"
android:textColor="#000"
android:textSize="17dp" />
</LinearLayout>

<!-- Footer aligned to bottom -->

<LinearLayout
android:id="@+id/footer"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@android:color/black"
android:visibility="visible" >

<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="Accept"
android:textColor="#FFFFFF"
android:visibility="visible" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="Decline"
android:textColor="#FFFFFF"

android:visibility="visible" />

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="Completed"
android:textColor="@android:color/white"
android:visibility="visible" />

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="Ongoing"
android:textColor="@android:color/white"
android:visibility="visible" />

<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="Decline"
android:textColor="@android:color/white"
android:visibility="visible" />
</LinearLayout>
<!-- Scrollable Item below header and above footer -->

<ScrollView
android:id="@+id/sl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/footer"
android:layout_below="@id/header"
android:background="@android:color/white" >

</ScrollView>

</RelativeLayout>

contents.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/editText04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Task 1"
android:textSize="17sp" >

<requestFocus />
</EditText>
</LinearLayout>

将您的 fragment 更改为

public class MyFragment extends Fragment implements OnClickListener {
EditText stat;
View view ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

view = inflater.inflate(R.layout.scollable_contents, null);

Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener(this);
ScrollView scrollable_contents = ( ScrollView)view.findViewById(R.id.sl);
LinearLayout v2 =(LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.contents, null);
stat=(EditText)v2.findViewById(R.id.editText04);
scrollable_contents.addView(v2);

return view;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
Toast.makeText(getActivity(),"accept clicked",1000).show();
stat.setText("Paused");
break;
}
}
}

快照

enter image description here

注意:

textSize 使用 sp 而不是 dp

关于android - 为什么在尝试在 ScrollView 中单击按钮时设置 EditText 时会出现 Nullpointer 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23780183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com