gpt4 book ai didi

java - Android setOnClickListener 不工作

转载 作者:行者123 更新时间:2023-11-29 03:21:48 26 4
gpt4 key购买 nike

在 android Activity.java 应用程序中添加按钮操作后不幸关闭。请帮助解决。我在这里缺少什么代码。????

public class MainActivity extends ActionBarActivity {


int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.textDisp);

add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
counter++;
}
});

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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_main, container, false);
return rootView;
}
}

}

最佳答案

您似乎在 fragment_main.xml 而不是 activity_main.xml 中构建了 View 。

当您第一次创建一个新的 android 项目时,您将拥有这些自动创建和打开的文件:

enter image description here

然后,当您开始时,在 fragment_main.xml 文件中添加 View (例如:TextView)。虽然您尝试在 Activity 中使用此 View 执行一个基本事件,但如下所示:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); // Using layout activity_main.xml

// You try to set a simple text on the view (TextView) previously added
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("Simple Text"); // And you get an error here!

/*
* You do an fragment transaction to add PlaceholderFragment Fragment
* on screen - this below snippnet is automatically created.
*/
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

您无法运行您的应用程序,或者有时只有白屏,因为您尝试调用/显示的 View 布局错误。

Solution: Move all your stuff inside onCreateView method into the Fragment class. Call views and do something in the related fragment and not the parent activity.


例如,对于您的情况:

public static class PlaceholderFragment extends Fragment {

int counter;
Button add, sub;
TextView display;

public PlaceholderFragment() {
}

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

counter = 0;
// Don't forget to attach your view to the inflated view as "rootView.findViewById()"
add = (Button) rootView.findViewById(R.id.bAdd);
sub = (Button) rootView.findViewById(R.id.bSub);
display = (TextView) rootView.findViewById(R.id.textDisp);

add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
counter++;
}
});
return rootView;
}
}

关于java - Android setOnClickListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23172934/

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