删除按钮一点击就闪退,怎么回事?
图一为用switch case方式界面没出来就闪退,所以两个按钮都不行;图二为在布局添加onClick方法在点击删除按钮时闪退,回退按钮可以用。
0
收起
正在回答
5回答
你是说
R.id.user可以监听成功,而R.id.back监听不成功吗?你测试一下,能不能监听到事件,你给88行换个别的代码,例如Toast显示一个字符串,如果显示不出来,则证明,你的xml中,与你写的id不统一。如果能监听到,则证明88到93行代码的问题。你需要缩小范围排查。
LexieMIZUKI
2019-04-18 16:26:18
LexieMIZUKI
2019-04-12 19:45:56
import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageButton; import com.lexieluv.imooc_note.MainActivity; import com.lexieluv.imooc_note.R; public class plus_fragment extends Fragment { private ImageButton back; private ImageButton clear; private EditText title; private EditText content; // Context context; // protected all_fragment af = new all_fragment(); // protected plus_fragment pf = new plus_fragment(); // protected user_fragment uf = new user_fragment(); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.plus_fragment,container,false); // this.getSupportFragmentManager() // .beginTransaction() // .add(R.id.container_content,af) // .add(R.id.container_content,pf) // .hide(pf) // .add(R.id.container_content,uf) // .hide(uf) // .commit(); } @Override public void onActivityCreated(@Nullable Bundle saveInstanceState) { super.onActivityCreated(saveInstanceState); initView(); } private void initView() { title = getView().findViewById(R.id.title); content = getView().findViewById(R.id.content); clear = clear.findViewById(R.id.clear); back = back.findViewById(R.id.back); clear.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { title.setText(""); content.setText(""); } }); back.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent backk=new Intent(getActivity(),MainActivity.class); startActivity(backk); // this.getSupportFragmentManager() // .beginTransaction() // .show(af) // .hide(pf) // .hide(uf) // .commit(); } }); } }
LexieMIZUKI
2019-04-09 18:23:36
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView; import com.lexieluv.imooc_note.fragment.all_fragment; import com.lexieluv.imooc_note.fragment.plus_fragment; import com.lexieluv.imooc_note.fragment.user_fragment; public class MainActivity extends AppCompatActivity implements View.OnClickListener { protected ImageButton all; protected ImageButton plus; protected ImageButton user; private ImageButton back; private ImageButton clear; private EditText title; private EditText content; protected all_fragment af = new all_fragment(); protected plus_fragment pf = new plus_fragment(); protected user_fragment uf = new user_fragment(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.getSupportFragmentManager() .beginTransaction() .add(R.id.container_content,af) .add(R.id.container_content,pf) .hide(pf) .add(R.id.container_content,uf) .hide(uf) .commit(); initView(); } private void initView() { all = this.findViewById(R.id.all); plus = this.findViewById(R.id.plus); user = this.findViewById(R.id.user); back = this.findViewById(R.id.back); clear = this.findViewById(R.id.clear); // title = this.findViewById(R.id.title); // content = this.findViewById(R.id.content); // back.setOnClickListener(this); // clear.setOnClickListener(this); all.setOnClickListener(this); plus.setOnClickListener(this); user.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.all: this.getSupportFragmentManager() .beginTransaction() .show(af) .hide(pf) .hide(uf) .commit(); break; case R.id.plus: this.getSupportFragmentManager() .beginTransaction() .show(pf) .hide(af) .hide(uf) .commit(); break; case R.id.user: this.getSupportFragmentManager() .beginTransaction() .show(uf) .hide(pf) .hide(af) .commit(); break; // case R.id.back: // this.getSupportFragmentManager() // .beginTransaction() // .show(af) // .hide(pf) // .hide(uf) // .commit(); // break; // case R.id.clear: // title.setText(""); // content.setText(""); // break; } } public void backClick(View view){ this.getSupportFragmentManager() .beginTransaction() .show(af) .hide(pf) .hide(uf) .commit(); } public void clearClick(View view){ title = findViewById(R.id.title); content = findViewById(R.id.content); title.setText(""); content.setText(""); } }
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星