gpt4 book ai didi

java - 为什么 setOnClick 会使这个应用程序崩溃?

转载 作者:行者123 更新时间:2023-12-03 00:56:37 27 4
gpt4 key购买 nike

我是 android 新手(对于 java 来说并不是完全陌生)。尝试遵循有点过时的教程。我不明白为什么 setOnClickListener 会使这个应用程序崩溃。我知道某处存在空指针异常,但我不知道如何修复它。另外,旁注,为什么我的类扩展 ActionBarActivity 而不是像我到目前为止看到的所有示例中那样扩展 Activity?

public class StartingPoint extends ActionBarActivity {

int counter;
Button add, sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);

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

counter = 0;

add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);

add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
counter++;
display.setText("Counter equals" + counter);

}
});


} //



@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends 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;
}
}
}

最佳答案

您的按钮和 TextView 位于 fragment 布局中,而不是 Activity 布局中,因此您必须将代码移至 onCreateView:

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

int counter=0;

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

Button add = (Button) rootView.findViewById(R.id.bAdd);
Button sub = (Button) rootView.findViewById(R.id.bSub);
TextView display = (TextView) rootView.findViewById(R.id.tvDisplay);

add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Counter equals" + counter);
}
});

return rootView;
}
}

另外,你必须清除你的onCreate,就离开:

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

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

关于java - 为什么 setOnClick 会使这个应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441154/

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