or null if no String value was found.
Aber durch
1name = "schonregistriert!!";
wird doch selbst im Fehlerfall ein Wert gesetzt? Daran sollte es doch dadurch nicht liegen?
Der komplette Code meiner Mainactivity ist folgender:
1package com.androidhive.pushnotifications;
2
3import static com.androidhive.pushnotifications.CommonUtilities.DISPLAY_MESSAGE_ACTION;
4import static com.androidhive.pushnotifications.CommonUtilities.EXTRA_MESSAGE;
5import static com.androidhive.pushnotifications.CommonUtilities.SENDER_ID;
6import android.R;
7import android.app.Activity;
8import android.content.BroadcastReceiver;
9import android.content.Context;
10import android.content.Intent;
11import android.content.IntentFilter;
12import android.os.AsyncTask;
13import android.os.Bundle;
14import android.util.Log;
15import android.webkit.WebSettings;
16import android.webkit.WebSettings.ZoomDensity;
17import android.webkit.WebView;
18import android.webkit.WebViewClient;
19import android.widget.TextView;
20import android.widget.Toast;
21
22import com.google.android.gcm.GCMRegistrar;
23
24public class MainActivity extends Activity {
25 // label to display gcm messages
26 TextView lblMessage;
27
28 // Asyntask
29 AsyncTask<Void, Void, Void> mRegisterTask;
30
31 // Alert dialog manager
32 AlertDialogManager alert = new AlertDialogManager();
33
34 // Connection detector
35 ConnectionDetector cd;
36
37 public static String name;
38 public static String email;
39
40 @Override
41 public void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 setContentView(com.androidhive.pushnotifications.R.layout.activity_main);
44
45
46 try{
47 cd = new ConnectionDetector(getApplicationContext());
48
49 // Check if Internet present
50 if (!cd.isConnectingToInternet()) {
51 // Internet Connection is not present
52 alert.showAlertDialog(MainActivity.this,
53 "Internet Connection Error",
54 "Please connect to working Internet connection", false);
55 // stop executing code by return
56 return;
57 }
58 }catch (Exception e) {
59
60 }
61
62
63 // Getting name,email from intent
64
65
66
67 try {
68 Intent i = getIntent();
69 name = i.getStringExtra("name");
70 email = i.getStringExtra("email");
71 } catch (Exception e) {
72 //System.out.println("Da ist wohl ein Fehler aufgetreten");
73 name = "schonregistriert!!";
74 email = "egal";
75 }
76
77
78
79 if (name.equals("schonregistriert!!")){
80 }else{
81 // Make sure the device has the proper dependencies.
82 GCMRegistrar.checkDevice(this);
83
84 // Make sure the manifest was properly set - comment out this line
85 // while developing the app, then uncomment it when it's ready.
86 GCMRegistrar.checkManifest(this);
87
88 //lblMessage = (TextView) findViewById(R.id.lblMessage);
89
90 registerReceiver(mHandleMessageReceiver, new IntentFilter(
91 DISPLAY_MESSAGE_ACTION));
92
93 // Get GCM registration id
94 final String regId = GCMRegistrar.getRegistrationId(this);
95
96 // Check if regid already presents
97 if (regId.equals("")) {
98 // Registration is not present, register now with GCM
99 GCMRegistrar.register(this, SENDER_ID);
100 } else {
101 // Device is already registered on GCM
102 if (GCMRegistrar.isRegisteredOnServer(this)) {
103 // Skips registration.
104 Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
105 } else {
106 // Try to register again, but not in the UI thread.
107 // It's also necessary to cancel the thread onDestroy(),
108 // hence the use of AsyncTask instead of a raw thread.
109 final Context context = this;
110 mRegisterTask = new AsyncTask<Void, Void, Void>() {
111
112 @Override
113 protected Void doInBackground(Void... params) {
114 // Register on our server
115 // On server creates a new user
116 ServerUtilities.register(context, name, email, regId);
117 return null;
118 }
119
120 @Override
121 protected void onPostExecute(Void result) {
122 mRegisterTask = null;
123 }
124
125 };
126 mRegisterTask.execute(null, null, null);
127 }
128 }
129 }
130
131 String url = "url";
132
133 WebView wbb = (WebView) findViewById(com.androidhive.pushnotifications.R.id.webView1);
134 wbb.setWebViewClient(new WebViewClient());
135 wbb.loadUrl(url);
136 //wbb.getSettings().setDefaultZoom(ZoomDensity.FAR);
137
138 wbb.getSettings().setLoadWithOverviewMode(true);
139 wbb.getSettings().setUseWideViewPort(true);
140
141 wbb.getSettings().setBuiltInZoomControls(true);
142
143
144 }
145
146 /**
147 * Receiving push messages
148 * */
149 private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
150 @Override
151 public void onReceive(Context context, Intent intent) {
152 String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
153 // Waking up mobile if it is sleeping
154 WakeLocker.acquire(getApplicationContext());
155
156 /**
157 * Take appropriate action on this message
158 * depending upon your app requirement
159 * For now i am just displaying it on the screen
160 * */
161
162 // Showing received message
163 //lblMessage.append(newMessage + "\n");
164 //Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
165
166 // Releasing wake lock
167 WakeLocker.release();
168 }
169 };
170
171 @Override
172 protected void onDestroy() {
173 if (mRegisterTask != null) {
174 mRegisterTask.cancel(true);
175 }
176 try {
177 unregisterReceiver(mHandleMessageReceiver);
178 GCMRegistrar.onDestroy(this);
179 } catch (Exception e) {
180 Log.e("UnRegister Receiver Error", "> " + e.getMessage());
181 }
182 super.onDestroy();
183 }
184
185}
Habe soviel wie möglich jetzt aus einem Beispielcode behalten, um Fehler von meiner Seite vorzubeugen.
Dabei gibts immer noch den Fehler. Den genauen Logg zu exakt diesem Code mit richtigen Zeilen editiere ich gleich rein.
Der LogCat gibt:
111-29 18:40:16.641: V/GCMBroadcastReceiver(1006): onReceive: com.google.android.c2dm.intent.RECEIVE
211-29 18:40:16.641: V/GCMBroadcastReceiver(1006): GCM IntentService class: com.androidhive.pushnotifications.GCMIntentService
311-29 18:40:16.651: V/GCMBaseIntentService(1006): Acquiring wakelock
411-29 18:40:16.682: V/GCMBaseIntentService(1006): Intent service name: GCMIntentService-693398460593-1
511-29 18:40:16.691: I/GCMIntentService(1006): Received message
611-29 18:40:16.901: V/GCMBaseIntentService(1006): Releasing wakelock
711-29 18:40:23.140: D/dalvikvm(1006): GC_FOR_ALLOC freed 75K, 8% free 2695K/2904K, paused 203ms, total 208ms
811-29 18:40:23.201: I/dalvikvm-heap(1006): Grow heap (frag case) to 3.365MB for 635812-byte allocation
911-29 18:40:23.321: D/dalvikvm(1006): GC_FOR_ALLOC freed 2K, 7% free 3313K/3528K, paused 119ms, total 119ms
1011-29 18:40:23.661: D/AndroidRuntime(1006): Shutting down VM
1111-29 18:40:23.661: W/dalvikvm(1006): threadid=1: thread exiting with uncaught exception (group=0x41465700)
1211-29 18:40:23.801: E/AndroidRuntime(1006): FATAL EXCEPTION: main
1311-29 18:40:23.801: E/AndroidRuntime(1006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.pushnotifications/com.androidhive.pushnotifications.MainActivity}: java.lang.NullPointerException
1411-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
1511-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
1611-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.ActivityThread.access$600(ActivityThread.java:141)
1711-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
1811-29 18:40:23.801: E/AndroidRuntime(1006): at android.os.Handler.dispatchMessage(Handler.java:99)
1911-29 18:40:23.801: E/AndroidRuntime(1006): at android.os.Looper.loop(Looper.java:137)
2011-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.ActivityThread.main(ActivityThread.java:5103)
2111-29 18:40:23.801: E/AndroidRuntime(1006): at java.lang.reflect.Method.invokeNative(Native Method)
2211-29 18:40:23.801: E/AndroidRuntime(1006): at java.lang.reflect.Method.invoke(Method.java:525)
2311-29 18:40:23.801: E/AndroidRuntime(1006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
2411-29 18:40:23.801: E/AndroidRuntime(1006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
2511-29 18:40:23.801: E/AndroidRuntime(1006): at dalvik.system.NativeStart.main(Native Method)
2611-29 18:40:23.801: E/AndroidRuntime(1006): Caused by: java.lang.NullPointerException
2711-29 18:40:23.801: E/AndroidRuntime(1006): at com.androidhive.pushnotifications.MainActivity.onCreate(MainActivity.java:79)
2811-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.Activity.performCreate(Activity.java:5133)
2911-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
3011-29 18:40:23.801: E/AndroidRuntime(1006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
3111-29 18:40:23.801: E/AndroidRuntime(1006): ... 11 more
Tut mir wirklich Leid für den langen Code. :/
— geändert am 30.11.2013, 00:42:22
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.