gpt4 book ai didi

java - Android onclickEventListener 异常

转载 作者:行者123 更新时间:2023-12-01 12:59:45 24 4
gpt4 key购买 nike

我有以下代码

public class StartingPoint extends ActionBarActivity {

int counter;
Button add, substract;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);

counter = 0;
add = (Button) findViewById(R.id.btn_add);
substract = (Button) findViewById(R.id.btn_subscract);
display = (TextView) findViewById(R.id.txt_display);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
counter++;
display.setText("Your total is " + counter);
}
});
}

尝试运行它时遇到各种异常:

05-11 21:23:47.604: E/AndroidRuntime(1944): java.lang.RuntimeException: Unable to start activity ComponentInfo{l.r.thenewboston/l.r.thenewboston.StartingPoint}: java.lang.NullPointerException
05-11 21:23:47.604: E/AndroidRuntime(1944): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

这里出了什么问题?

Activity 起点布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="l.r.thenewboston.StartingPoint"
tools:ignore="MergeRootFrame" />

和 fregment_starting_point 布局

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="l.r.thenewboston.StartingPoint$PlaceholderFragment" >

<TextView
android:id="@+id/txt_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/total_text"
android:textSize="44sp"
android:layout_gravity="center"
android:gravity="center"/>

<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_display"
android:layout_below="@+id/txt_display"
android:layout_marginTop="19dp"
android:textSize="18sp"
android:text="@string/add_one" />

<Button
android:id="@+id/btn_subscract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_add"
android:layout_alignBottom="@+id/btn_add"
android:layout_alignRight="@+id/txt_display"
android:text="@string/minus_one"
android:textSize="18sp" />

公共(public)静态类 PlaceholderFragment 扩展 Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_starting_point,
container, false);
return rootView;
}

}

最佳答案

问题是您正在尝试访问 Fragment Layout 中的 View 来自Activity .

这是不可能的。 。您的Activity Layout只有一个容器来容纳Fragment .所以Activity onCreate应该是只添加 Fragment

将您的代码移至 Activity onCreateFragment onCreateView像下面这样

您可以访问ButtonsTextViewFragment Layout来自PlaceholderFragmentonCreateView Fragment Layout inflate之后

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fregment_starting_point, container,false);

dd = (Button) rootView.findViewById(R.id.btn_add);
substract = (Button) rootView.findViewById(R.id.btn_subscract);
display = (TextView) rootView.findViewById(R.id.txt_display);

add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
counter++;
display.setText("Your total is " + counter);
}
});

return rootView;
}

关于java - Android onclickEventListener 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598286/

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