android,点击"确认",就崩溃了

YXTS122 2018-02-18 03:52:54
public class MainActivity extends Activity implements OnClickListener {
EditText editText;
Button bt;
TextView textView,textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.edittext);
bt=(Button)findViewById(R.id.button);
textView=(TextView)findViewById(R.id.tv1);
textView1=(TextView)findViewById(R.id.tv2);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:

String str=editText.getText().toString();
textView.setText(str+"以内的质数有:");
textView1.setText("2,3");
int n=Integer.parseInt(str);
int i=5;
int j=2;
while(i<=n)
{
while(j<i)
{
j=2;
if(i%j==0)
break;
if(i-1==j)
textView1.append(","+i);
j++;
}
i++;
}

break;
default:break;
}
}
}


<LinearLayout 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"
tools:context="com.example.prime.MainActivity"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入一个数:" />
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
</LinearLayout>

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>



...全文
1022 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2018-02-21
  • 打赏
  • 举报
回复
程序死循环,并不是崩溃,改为:
public class MainActivity extends Activity implements OnClickListener {
    EditText editText;
    Button bt;
    TextView textView,textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=(EditText)findViewById(R.id.edittext);
        bt=(Button)findViewById(R.id.button);
        textView=(TextView)findViewById(R.id.tv1);
        textView1=(TextView)findViewById(R.id.tv2);
        bt.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button:
             
            String str=editText.getText().toString();
            textView.setText(str+"以内的质数有:");
            textView1.setText("2,3");
            try{
            int n=Integer.parseInt(str);
            int i=5;
            int j;
            while(i<=n)
            {
            	j=2;
                while(j<i)
                {
                    if(i%j==0)
                        break;
                    if(i-1==j)
                        textView1.append(","+i);
                    j++;
                }
                i++;
            }
            }
            catch (Exception e)
            {
            	textView1.setText(e.toString());
            }
             
            break;
       default:break;
        }
    }
}  

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧