Форум программистов, компьютерный форум, киберфорум
Программирование Android
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
2 / 2 / 0
Регистрация: 21.11.2012
Сообщений: 207
1

Приложение заверашется при выполнении кода

07.03.2016, 11:26. Показов 496. Ответов 10
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Доброго времени суток!Пишу калькулятор. В нем два хml файла. MainActivity и layout.xml Возникла проблема: когда выполняется вот этот код программа завершается.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Button [] lstbut_digi[10]= new Button[10];
lstbut_digi[0]=(Button)findViewById(R.id.bt0);
        lstbut_digi[1]=(Button)findViewById(R.id.bt1);
        lstbut_digi[2]=(Button)findViewById(R.id.bt2);
        lstbut_digi[3]=(Button)findViewById(R.id.bt3);
        lstbut_digi[4]=(Button)findViewById(R.id.bt4);
        lstbut_digi[5]=(Button)findViewById(R.id.bt5);
        lstbut_digi[6]=(Button)findViewById(R.id.bt6);
        lstbut_digi[7]=(Button)findViewById(R.id.bt7);
        lstbut_digi[8]=(Button)findViewById(R.id.bt8);
        lstbut_digi[9]=(Button)findViewById(R.id.bt9);
        for (int i=0;i<10;i++ ) {
            lstbut_digi[i].setOnClickListener(this);
        }
Изначально lstbut_digi был списком а цилк выглядел так:
Java
1
2
3
  for (Button b:    lstbut_digi ) {
            b.setOnClickListener(this);
        }


Вот на всякий случай весь код проекта:
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.example.bjornskau.lesson19;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;
 
import org.w3c.dom.Text;
 
import java.util.List;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText etN1;
    EditText etN2;
    Button btnAdd;
    Button btnSub;
    Button btnMult;
    Button btnDiv;
    TextView tvRes;
 //   Button bt7;
    TableRow row1;
    EditText etNUM;
 public Button[] lstbut_digi= new Button[10];
boolean view_swch=false;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etN1= (EditText)findViewById(R.id.etNum1);
        etN2= (EditText)findViewById(R.id.etNum2);
        btnAdd =(Button)findViewById(R.id.btnAdd);
        btnDiv=(Button)findViewById(R.id.btnDiv);
        btnSub=(Button)findViewById(R.id.btnSub);
        btnMult=(Button)findViewById(R.id.btnMult);
        tvRes =(TextView)findViewById(R.id.tvResult);
        row1=(TableRow)findViewById(R.id.row1);
        etNUM=(EditText)findViewById(R.id.etNum);
 
        lstbut_digi[0]=(Button)findViewById(R.id.bt0);
        lstbut_digi[1]=(Button)findViewById(R.id.bt1);
        lstbut_digi[2]=(Button)findViewById(R.id.bt2);
        lstbut_digi[3]=(Button)findViewById(R.id.bt3);
        lstbut_digi[4]=(Button)findViewById(R.id.bt4);
        lstbut_digi[5]=(Button)findViewById(R.id.bt5);
        lstbut_digi[6]=(Button)findViewById(R.id.bt6);
        lstbut_digi[7]=(Button)findViewById(R.id.bt7);
        lstbut_digi[8]=(Button)findViewById(R.id.bt8);
        lstbut_digi[9]=(Button)findViewById(R.id.bt9);
        for (int i=0;i<10;i++ ) {
            lstbut_digi[i].setOnClickListener(this);
        }
        btnAdd.setOnClickListener(this);
        btnDiv.setOnClickListener(this);
        btnMult.setOnClickListener(this);
        btnSub.setOnClickListener(this);
 
 
 
    }
 
    @Override
    public  void onClick(View v)
{
    float res=0;
    float n=Float.parseFloat(etN1.getText().toString());
    float n2=Float.parseFloat(etN2.getText().toString());
    switch (v.getId())
    {
 
        case R.id.btnAdd:
        {
            res=n+n2;
          break;
        }
        case R.id.btnMult:
        {
           res=n*n2;
            break;
        }
        case R.id.btnSub:
        {
 
            res=n-n2;
            break;
        }
        case R.id.btnDiv:
        {
            res=n/n2;
            break;
        }
        case R.id.bt0:
        {
          etNUM.setText(etNUM.getText().toString().concat("0"));break;
        }
        case R.id.bt1:
        {
            etNUM.setText(etNUM.getText().toString().concat("1"));break;
        }
        case R.id.bt2:
        {
            etNUM.setText(etNUM.getText().toString().concat("2"));break;
        }
        case R.id.bt3:
        {
            etNUM.setText(etNUM.getText().toString().concat("3"));break;
        }
        case R.id.bt4:
        {
            etNUM.setText(etNUM.getText().toString().concat("4"));break;
        }
        case R.id.bt5:
        {
            etNUM.setText(etNUM.getText().toString().concat("5"));break;
        }
        case R.id.bt6:
        {
            etNUM.setText(etNUM.getText().toString().concat("6"));break;
        }
        case R.id.bt7:
        {
            etNUM.setText(etNUM.getText().toString().concat("7"));break;
        }
        case R.id.bt8:
        {
            etNUM.setText(etNUM.getText().toString().concat("8"));break;
        }
        case R.id.bt9:
        {
            etNUM.setText(etNUM.getText().toString().concat("9"));break;
        }
 
 
    }
    tvRes.setText(String.valueOf(res));
}
    @Override
    public  boolean onCreateOptionsMenu(Menu menu)
    {
 
        menu.add(0,1,0,"Advanced calc");
        return  super.onCreateOptionsMenu(menu);
    }
    @Override
    public  boolean onOptionsItemSelected(MenuItem it)
    {
        switch (it.getItemId())
        {
 
            case  1:
            { if (view_swch==false){
                setContentView(R.layout.layout1);
                it.setTitle("Simple calc");
                view_swch=true;}
                else
              {
 
               setContentView(R.layout.activity_main);
                  view_swch=false;
                  it.setTitle("Advanced calc");
              }
            }
        }
        return  super.onOptionsItemSelected(it);
    }
 
}
Вот содержимое layout1.xml:
XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:layout_marginLeft="10pt"
        android:layout_marginRight="10pt"
        android:layout_marginTop="3pt">
        <EditText
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginRight="5pt"
            android:id="@+id/etNum"
            android:layout_width="match_parent"
            android:inputType="numberDecimal">
        </EditText>
 
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:layout_marginTop="3pt"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt">
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tll_with_buttins">
            <TableRow  android:id="@+id/row1">
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="0"
                    android:textSize="8pt"
                    android:id="@+id/bt0">
                </Button>
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="1"
                    android:textSize="8pt"
                    android:id="@+id/bt1">
                </Button>
 
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="2"
                    android:textSize="8pt"
                    android:id="@+id/bt2">
                </Button>
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="3"
                    android:textSize="8pt"
                    android:id="@+id/bt3">
                </Button>
 
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="4"
                    android:textSize="8pt"
                    android:id="@+id/bt4">
                </Button>
 
            </TableRow>
 
            <TableRow>
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="5"
                    android:textSize="8pt"
                    android:id="@+id/bt5">
                </Button>
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="6"
                    android:textSize="8pt"
                    android:id="@+id/bt6">
                </Button>
 
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="7"
                    android:textSize="8pt"
                    android:id="@+id/bt7">
                </Button>
 
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="8"
                    android:textSize="8pt"
                    android:id="@+id/bt8">
                </Button>
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:text="9"
                    android:textSize="8pt"
                    android:id="@+id/bt9">
                </Button>
 
 
 
            </TableRow>
            <TableRow>
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:text="+"
                    android:textSize="8pt"
                    android:id="@+id/bAdd">
                </Button>
 
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:text="-"
                    android:textSize="8pt"
                    android:id="@+id/bSub">
                </Button>
 
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:text="*"
                    android:textSize="8pt"
                    android:id="@+id/bMult">
                </Button>
 
                <Button
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:text="/"
                    android:textSize="8pt"
                    android:id="@+id/bDiv">
                </Button>
 
            </TableRow>
            <TableRow>   <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_marginLeft="5pt"
                android:layout_marginRight="5pt"
                android:textSize="12pt"
                android:layout_marginTop="3pt"
                android:id="@+id/tvResult"
                android:gravity="center_horizontal">
            </TextView></TableRow>
 
        </TableLayout>
 
 
 
    </LinearLayout>
 
</LinearLayout>
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
07.03.2016, 11:26
Ответы с готовыми решениями:

Ошибка при выполнении кода VBA при авторизации пользователя
Добрый день! Для создания авторизации пользователей в базе взяла готовый код VBA и попробовала...

Ошибка при выполнении кода
При работе на сервере АЯКС все работает нормально, но на денвере выбивает следующие ошибки:...

Ошибка при выполнении кода
Здравствуйте! Столкнулся с такой проблемой: в строчке &quot;if (isset($_POST)) { //кабинет&quot; не...

Ошибка при выполнении кода
При написания кода на использование данных из ТЧ справочника в ТЧ документа возникает вот такая...

10
2883 / 2295 / 769
Регистрация: 12.05.2014
Сообщений: 7,978
07.03.2016, 11:50 2
надо код ошибки из логов
0
2 / 2 / 0
Регистрация: 21.11.2012
Сообщений: 207
07.03.2016, 18:13  [ТС] 3
Вот скриншоты логов
Миниатюры
Приложение заверашется при выполнении кода  
0
2883 / 2295 / 769
Регистрация: 12.05.2014
Сообщений: 7,978
07.03.2016, 18:35 4
в onCreate MainActivity написано setContentView(R.layout.activity_main);
а в тексте выше приведет файл layout1.xml

должен быть как и написано - activity_main или надо исправлять в onCreate на нужный лаяут
0
2 / 2 / 0
Регистрация: 21.11.2012
Сообщений: 207
07.03.2016, 18:50  [ТС] 5
Main Activtu.xml не привел так как в сообщении мне не хватало символов. Эмпирически выяснил что ошибку вызывывает вот этот код
Java
1
2
3
 for (Button b:    lstbut_digi ) {
            b.setOnClickListener(this);
        }
При чем даже когда я создал отдельынй OnClickListener и убрал цикл:
Java
1
2
3
4
5
6
7
8
9
10
11
12
        bt0=(Button)findViewById(R.id.bt0);
        bt1=(Button)findViewById(R.id.bt1);
        bt2=(Button)findViewById(R.id.bt2);
        bt3=(Button)findViewById(R.id.bt3);
        bt4=(Button)findViewById(R.id.bt4);
        bt5=(Button)findViewById(R.id.bt5);
        bt6=(Button)findViewById(R.id.bt6);
        bt7=(Button)findViewById(R.id.bt7);
        bt8=(Button)findViewById(R.id.bt8);
        bt9=(Button)findViewById(R.id.bt9);
bt0.setOnClickListener(ocl1);
        bt2.setOnClickListener(ocl1);
ничего не изменилось ....
Миниатюры
Приложение заверашется при выполнении кода  
0
2883 / 2295 / 769
Регистрация: 12.05.2014
Сообщений: 7,978
07.03.2016, 18:55 6
ну и где activity_main.xml ?
0
2 / 2 / 0
Регистрация: 21.11.2012
Сообщений: 207
07.03.2016, 18:57  [ТС] 7
Вот ...

XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:layout_marginLeft="10pt"
        android:layout_marginRight="10pt"
        android:layout_marginTop="3pt">
        <EditText
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginRight="5pt"
            android:id="@+id/etNum1"
            android:layout_width="match_parent"
            android:inputType="numberDecimal">
        </EditText>
        <EditText
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="5pt"
            android:id="@+id/etNum2"
            android:layout_width="match_parent"
            android:inputType="numberDecimal">
        </EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:layout_marginTop="3pt"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="+"
            android:textSize="8pt"
            android:id="@+id/btnAdd">
        </Button>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="-"
            android:textSize="8pt"
            android:id="@+id/btnSub">
        </Button>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="*"
            android:textSize="8pt"
            android:id="@+id/btnMult">
        </Button>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="/"
            android:textSize="8pt"
            android:id="@+id/btnDiv">
        </Button>
    </LinearLayout>
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
        android:textSize="12pt"
        android:layout_marginTop="3pt"
        android:id="@+id/tvResult"
        android:gravity="center_horizontal">
    </TextView>
</LinearLayout>
0
2883 / 2295 / 769
Регистрация: 12.05.2014
Сообщений: 7,978
07.03.2016, 19:00 8
в нем же нет ни одной кнопки с id от 0 до 9
0
2 / 2 / 0
Регистрация: 21.11.2012
Сообщений: 207
07.03.2016, 19:04  [ТС] 9
Дык они в layout1.xml У меня по нажатию пункта меню открывается layoutq.xml и с ним наичнается взаимодействие...
0
2883 / 2295 / 769
Регистрация: 12.05.2014
Сообщений: 7,978
07.03.2016, 19:10 10
тогда и кнопки надо искать после того как был установлен новый лаяут
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
lstbut_digi[0]=(Button)findViewById(R.id.bt0);
        lstbut_digi[1]=(Button)findViewById(R.id.bt1);
        lstbut_digi[2]=(Button)findViewById(R.id.bt2);
        lstbut_digi[3]=(Button)findViewById(R.id.bt3);
        lstbut_digi[4]=(Button)findViewById(R.id.bt4);
        lstbut_digi[5]=(Button)findViewById(R.id.bt5);
        lstbut_digi[6]=(Button)findViewById(R.id.bt6);
        lstbut_digi[7]=(Button)findViewById(R.id.bt7);
        lstbut_digi[8]=(Button)findViewById(R.id.bt8);
        lstbut_digi[9]=(Button)findViewById(R.id.bt9);
        for (int i=0;i<10;i++ ) {
            lstbut_digi[i].setOnClickListener(this);
        }
        btnAdd.setOnClickListener(this);
        btnDiv.setOnClickListener(this);
        btnMult.setOnClickListener(this);
        btnSub.setOnClickListener(this);
все это вынести в какой-нибудь метод который будет вызываться отсюда
Java
1
2
3
4
5
6
7
case  1:
            { if (view_swch==false){
                setContentView(R.layout.layout1);
                it.setTitle("Simple calc");
                view_swch=true;}
                else
              {
а вообще это все криво до ужаса
фрагментами делать надо или как-то иначе, но не через смену лаяута налету
1
2 / 2 / 0
Регистрация: 21.11.2012
Сообщений: 207
07.03.2016, 19:13  [ТС] 11
Хм, посоветуете новичку, как именно?))
0
07.03.2016, 19:13
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
07.03.2016, 19:13
Помогаю со студенческими работами здесь

Ошибка при выполнении кода
Почему не работает такой код: def max_number(x, y): if x &gt; y: return x else: return...

Ошибка при выполнении кода
&quot;Вызов функции PInvoke &quot;TestApp!TestApp.IM+INIManager::GetIString&quot; разбалансировал стек. Вероятно,...

Среда не отвечает при выполнении кода
Здравствуйте.У меня проблема с Pascal,после нажатия &quot;Выполнить&quot; (F9) Pascal зависает.В чем...

При выполнении кода возникает ошибка
Здравствуйте, дорогие формучане! 1. При выполнении кода выдаёт ошибку if...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
11
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru