03-02 11:38:06.620: E/AndroidRuntime(840): FATAL EXCEPTION: main
03-02 11:38:06.620: E/AndroidRuntime(840): Process: de.cookiegames.sheep, PID: 840
03-02 11:38:06.620: E/AndroidRuntime(840): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.cookiegames.sheep/de.cookiegames.sheep.MainActivity}: java.lang.NullPointerException
03-02 11:38:06.620: E/AndroidRuntime(840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.os.Looper.loop(Looper.java:136)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-02 11:38:06.620: E/AndroidRuntime(840): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 11:38:06.620: E/AndroidRuntime(840): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 11:38:06.620: E/AndroidRuntime(840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-02 11:38:06.620: E/AndroidRuntime(840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-02 11:38:06.620: E/AndroidRuntime(840): at dalvik.system.NativeStart.main(Native Method)
03-02 11:38:06.620: E/AndroidRuntime(840): Caused by: java.lang.NullPointerException
03-02 11:38:06.620: E/AndroidRuntime(840): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
03-02 11:38:06.620: E/AndroidRuntime(840): at de.cookiegames.sheep.MainActivity.<init>(MainActivity.java:19)
03-02 11:38:06.620: E/AndroidRuntime(840): at java.lang.Class.newInstanceImpl(Native Method)
03-02 11:38:06.620: E/AndroidRuntime(840): at java.lang.Class.newInstance(Class.java:1208)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-02 11:38:06.620: E/AndroidRuntime(840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
03-02 11:38:06.620: E/AndroidRuntime(840): ... 11 more
Null Pointer Exception(mehr kann ich damit nicht anfangen)
1package de.cookiegames.sheep;
2
3
4
5import android.os.Bundle;
6import android.os.CountDownTimer;
7import android.preference.PreferenceManager;
8import android.app.Activity;
9import android.content.Context;
10import android.content.SharedPreferences;
11import android.view.Menu;
12import android.view.View;
13import android.widget.ImageButton;
14import android.widget.TextView;
15import android.widget.Toast;
16
17
18public class MainActivity extends Activity {
19 int score = getSharedPreferences("Score", MODE_PRIVATE).getInt("score", 0);
20 int punkte = 0;
21
22 final String[] schafe = {"1", "2", "3", "4", "5", "6", "7"};
23
24
25
26 @Override
27 protected void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.game);
30
31
32
33 }
34
35
36 @Override
37 public boolean onCreateOptionsMenu(Menu menu) {
38 // Inflate the menu; this adds items to the action bar if it is present.
39 getMenuInflater().inflate(R.menu.main, menu);
40 return true;
41 }
42
43
44
45
46
47
48
49
50 public void spielen (View view) {
51 setContentView(R.layout.activity_main);
52
53
54
55 new CountDownTimer(500, 1000) {
56 ImageButton schaf1 = (ImageButton) findViewById(R.id.imageButton1);
57 ImageButton schaf2 = (ImageButton) findViewById(R.id.imageButton2);
58 ImageButton schaf3 = (ImageButton) findViewById(R.id.imageButton3);
59 ImageButton schaf4 = (ImageButton) findViewById(R.id.ImageButton4);
60 ImageButton schaf5 = (ImageButton) findViewById(R.id.ImageButton5);
61 ImageButton schaf6 = (ImageButton) findViewById(R.id.ImageButton6);
62 ImageButton schaf7 = (ImageButton) findViewById(R.id.imageButton7);
63
64 TextView tv = (TextView)findViewById(R.id.textView1);
65
66 public void onTick(long millisUntilFinished) {
67
68 }
69
70 public void onFinish() {
71 int random = (int) (Math.random() *6 );
72 tv.setText(schafe[random]);
73
74 if (tv.getText().toString().equals("1"))
75 {
76 schaf1.setVisibility(View.VISIBLE);
77 }
78 if (tv.getText().toString().equals("2"))
79 {
80 schaf2.setVisibility(View.VISIBLE);
81 }
82 if (tv.getText().toString().equals("3"))
83 {
84 schaf3.setVisibility(View.VISIBLE);
85 }
86 if (tv.getText().toString().equals("4"))
87 {
88 schaf4.setVisibility(View.VISIBLE);
89 }
90 if (tv.getText().toString().equals("5"))
91 {
92 schaf5.setVisibility(View.VISIBLE);
93 }
94 if (tv.getText().toString().equals("6"))
95 {
96 schaf6.setVisibility(View.VISIBLE);
97 }
98 if (tv.getText().toString().equals("7"))
99 {
100 schaf7.setVisibility(View.VISIBLE);
101 }
102 this.start();
103
104
105 }
106 }.start();
107
108
109
110
111 new CountDownTimer(1000, 1000) {
112 ImageButton schaf1 = (ImageButton) findViewById(R.id.imageButton1);
113 ImageButton schaf2 = (ImageButton) findViewById(R.id.imageButton2);
114 ImageButton schaf3 = (ImageButton) findViewById(R.id.imageButton3);
115 ImageButton schaf4 = (ImageButton) findViewById(R.id.ImageButton4);
116 ImageButton schaf5 = (ImageButton) findViewById(R.id.ImageButton5);
117 ImageButton schaf6 = (ImageButton) findViewById(R.id.ImageButton6);
118 ImageButton schaf7 = (ImageButton) findViewById(R.id.imageButton7);
119 public void onFinish() {
120 schaf1.setVisibility(View.INVISIBLE);
121 schaf2.setVisibility(View.INVISIBLE);
122 schaf3.setVisibility(View.INVISIBLE);
123 schaf4.setVisibility(View.INVISIBLE);
124 schaf5.setVisibility(View.INVISIBLE);
125 schaf6.setVisibility(View.INVISIBLE);
126 schaf7.setVisibility(View.INVISIBLE);
127
128 this.start();
129
130 }
131
132 @Override
133 public void onTick(long millisUntilFinished) {
134
135
136 }
137
138 }.start();
139
140
141
142 new CountDownTimer(30000, 1000) {
143 TextView tv7 = (TextView) findViewById(R.id.textView7);
144 public void onTick(long millisUntilFinished) {
145 tv7.setText("Zeit:" + millisUntilFinished / 1000);
146 }
147
148 public void onFinish() {
149
150 score = getSharedPreferences("Score", MODE_PRIVATE).getInt("score", punkte);
151
152 if(punkte > score)
153 {
154 SharedPreferences preferences = getSharedPreferences("Score", Context.MODE_WORLD_WRITEABLE);
155 SharedPreferences.Editor editor = preferences.edit();
156 editor.putInt("score", punkte);
157
158 editor.commit();
159 }
160
161
162
163
164
165
166 setContentView(R.layout.game); //hier kommt noch ein Toast
167 Toast.makeText(getApplicationContext(), "Score:" + punkte, Toast.LENGTH_SHORT).show();
168 punkte = 0;
169
170
171
172
173
174
175
176 }
177
178 }.start();
179 }
180
181 public void schafklick1(View view){
182 TextView tv3 = (TextView) findViewById(R.id.textView3);
183 ImageButton schaf1 = (ImageButton) findViewById(R.id.imageButton1);
184 punkte += 10;
185 tv3.setText("Score:" + String.valueOf(punkte));
186 schaf1.setVisibility(View.INVISIBLE);
187 }
188 public void schafklick2(View view){
189 TextView tv3 = (TextView) findViewById(R.id.textView3);
190 ImageButton schaf2 = (ImageButton) findViewById(R.id.imageButton2);
191 punkte += 10;
192 tv3.setText("Score:" + String.valueOf(punkte));
193 schaf2.setVisibility(View.INVISIBLE);
194 }
195 public void schafklick3(View view){
196 TextView tv3 = (TextView) findViewById(R.id.textView3);
197 ImageButton schaf3 = (ImageButton) findViewById(R.id.imageButton3);
198 punkte += 10;
199 tv3.setText("Score:" + String.valueOf(punkte));
200 schaf3.setVisibility(View.INVISIBLE);
201 }
202 public void schafklick4(View view){
203 TextView tv3 = (TextView) findViewById(R.id.textView3);
204 ImageButton schaf4 = (ImageButton) findViewById(R.id.ImageButton4);
205 punkte += 10;
206 tv3.setText("Score:" + String.valueOf(punkte));
207 schaf4.setVisibility(View.INVISIBLE);
208 }
209 public void schafklick5(View view){
210 TextView tv3 = (TextView) findViewById(R.id.textView3);
211 ImageButton schaf5 = (ImageButton) findViewById(R.id.ImageButton5);
212 punkte += 10;
213 tv3.setText("Score:" + String.valueOf(punkte));
214 schaf5.setVisibility(View.INVISIBLE);
215 }
216 public void schafklick6(View view){
217 TextView tv3 = (TextView) findViewById(R.id.textView3);
218 ImageButton schaf6 = (ImageButton) findViewById(R.id.ImageButton6);
219 punkte += 10;
220 tv3.setText("Score:" + String.valueOf(punkte));
221 schaf6.setVisibility(View.INVISIBLE);
222 }
223 public void schafklick7(View view){
224 TextView tv3 = (TextView) findViewById(R.id.textView3);
225 ImageButton schaf7 = (ImageButton) findViewById(R.id.imageButton7);
226 punkte += 10;
227 tv3.setText("Score:" + String.valueOf(punkte));
228 schaf7.setVisibility(View.INVISIBLE);
229 }
230
231 public void score(View view) {
232
233 SharedPreferences prfs = getSharedPreferences("Score", Context.MODE_PRIVATE);
234 Toast.makeText(getApplicationContext(), "" + prfs.getString("score", ""), Toast.LENGTH_SHORT).show();
235
236
237
238
239 }
240}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.