Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.52 KB | None | 0 0
  1. FEHLER MELDUNG
  2. FATAL EXCEPTION: main
  3. Process: com.example.enesb.homesmarthomev2, PID: 20634
  4. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.enesb.homesmarthomev2/com.example.enesb.homesmarthomev2.raum4}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference
  5. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
  6. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
  7. at android.app.ActivityThread.access$1100(ActivityThread.java:221)
  8. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
  9. at android.os.Handler.dispatchMessage(Handler.java:102)
  10. at android.os.Looper.loop(Looper.java:158)
  11. at android.app.ActivityThread.main(ActivityThread.java:7224)
  12. at java.lang.reflect.Method.invoke(Native Method)
  13. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
  14. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
  15. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference
  16. at com.example.enesb.homesmarthomev2.raum4.onCreate(raum4.java:126)
  17. at android.app.Activity.performCreate(Activity.java:6876)
  18. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
  19. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
  20. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
  21. at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
  22. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
  23. at android.os.Handler.dispatchMessage(Handler.java:102) 
  24. at android.os.Looper.loop(Looper.java:158) 
  25. at android.app.ActivityThread.main(ActivityThread.java:7224) 
  26. at java.lang.reflect.Method.invoke(Native Method) 
  27. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
  28. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
  29. 02-25 23:49:53.924 20634-20634/com.example.enesb.homesmarthomev2 I/Process: Sending signal. PID: 20634 SIG: 9
  30.  
  31.  
  32.  
  33.  
  34.  
  35. ---------------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. package com.example.enesb.homesmarthomev2;
  38.  
  39. import android.content.Intent;
  40. import android.graphics.Color;
  41. import android.graphics.PorterDuff;
  42. import android.os.Bundle;
  43. import android.support.design.widget.FloatingActionButton;
  44. import android.support.design.widget.Snackbar;
  45. import android.view.View;
  46. import android.support.design.widget.NavigationView;
  47. import android.support.v4.view.GravityCompat;
  48. import android.support.v4.widget.DrawerLayout;
  49. import android.support.v7.app.ActionBarDrawerToggle;
  50. import android.support.v7.app.AppCompatActivity;
  51. import android.support.v7.widget.Toolbar;
  52. import android.view.Menu;
  53. import android.view.MenuItem;
  54. import android.widget.Button;
  55. import android.widget.SeekBar;
  56. import android.widget.Toast;
  57.  
  58. import org.eclipse.paho.android.service.MqttAndroidClient;
  59. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  60. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  61. import org.eclipse.paho.client.mqttv3.IMqttToken;
  62. import org.eclipse.paho.client.mqttv3.MqttCallback;
  63. import org.eclipse.paho.client.mqttv3.MqttClient;
  64. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  65. import org.eclipse.paho.client.mqttv3.MqttException;
  66. import org.eclipse.paho.client.mqttv3.MqttMessage;
  67. import org.eclipse.paho.client.mqttv3.util.Strings;
  68.  
  69. public class raum4 extends AppCompatActivity
  70. implements MqttCallback, NavigationView.OnNavigationItemSelectedListener {
  71.  
  72. SeekBar seek_helligkeit;
  73. SeekBar seek_rot, seek_gruen, seek_blau;
  74.  
  75. Button btn_helligkeit;
  76. int helligkeit = 0;
  77. int rot = 0;
  78. int gruen = 0;
  79. int blau = 0;
  80. int personen;
  81. int licht;
  82. int j;
  83.  
  84. static String MQTTHOST = "tcp://m20.cloudmqtt.com:19860";
  85. static String USERNAME = "phone";
  86. static String PASSWORD = "phonetest";
  87. public String msg;
  88. public String topic1;
  89.  
  90. public MqttAndroidClient client;
  91.  
  92. public int QOS = 1;
  93. public boolean KA = false;
  94.  
  95. @Override
  96. protected void onCreate(Bundle savedInstanceState) {
  97. super.onCreate(savedInstanceState);
  98. setContentView(R.layout.activity_raum4);
  99.  
  100. connect();
  101.  
  102. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  103. setSupportActionBar(toolbar);
  104.  
  105. btn_helligkeit =(Button) findViewById(R.id.btn_helligkeit);
  106. seek_helligkeit = (SeekBar) findViewById(R.id.seek_helligkeit);
  107.  
  108. seek_rot = (SeekBar) findViewById(R.id.seek_rot);
  109. seek_gruen = (SeekBar) findViewById(R.id.seek_gruen);
  110. seek_blau = (SeekBar) findViewById(R.id.seek_blau);
  111.  
  112. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  113. fab.setOnClickListener(new View.OnClickListener() {
  114. @Override
  115. public void onClick(View view) {
  116. if (licht == 1) {
  117. publish("Haus/Raum4/Licht", "0");
  118. Toast.makeText(raum4.this, "PENIS0", Toast.LENGTH_LONG).show();
  119. btn_helligkeit.getBackground().clearColorFilter();
  120. }
  121. if (licht == 0) {
  122. publish("Haus/Raum4/Licht", "1");
  123. Toast.makeText(raum4.this, "PENIS1", Toast.LENGTH_LONG).show();
  124. btn_helligkeit.getBackground().setColorFilter(0xffffff00, PorterDuff.Mode.MULTIPLY);
  125. }
  126. }
  127. });
  128.  
  129. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  130. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  131. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  132. drawer.setDrawerListener(toggle);
  133. toggle.syncState();
  134.  
  135. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  136. navigationView.setNavigationItemSelectedListener(this);
  137.  
  138. seek_helligkeit.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  139. @Override
  140. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  141. if(b){
  142. j++;
  143. if (j == 2 ){
  144. publish("Haus/Raum5/Helligkeit", String.valueOf(i));
  145. helligkeit = i;
  146. updateLicht();
  147. j = 0;
  148. }
  149. }
  150. }
  151.  
  152. @Override
  153. public void onStartTrackingTouch(SeekBar seekBar) {
  154. }
  155.  
  156. @Override
  157. public void onStopTrackingTouch(SeekBar seekBar) {
  158. KA = true;
  159. publish("Haus/Raum4/Helligkeit", String.valueOf(seekBar.getProgress()));
  160. }
  161. });
  162.  
  163. seek_rot.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
  164. @Override
  165. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  166. rot = 0;
  167. if (b){
  168. publish("Haus/Raum4/RGB/R", String.valueOf(i));
  169. rot = i;
  170. }
  171. }
  172.  
  173. @Override
  174. public void onStartTrackingTouch(SeekBar seekBar) {
  175.  
  176. }
  177.  
  178. @Override
  179. public void onStopTrackingTouch(SeekBar seekBar) {
  180. KA = true;
  181. publish("Haus/Raum4/RGB/R", String.valueOf(seekBar.getProgress()));
  182. }
  183. });
  184.  
  185. seek_gruen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
  186. @Override
  187. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  188. if (b){
  189. publish("Haus/Raum4/RGB/G", String.valueOf(i));
  190. gruen = i;
  191. }
  192. }
  193.  
  194. @Override
  195. public void onStartTrackingTouch(SeekBar seekBar) {
  196.  
  197. }
  198.  
  199. @Override
  200. public void onStopTrackingTouch(SeekBar seekBar) {
  201. KA = true;
  202. publish("Haus/Raum4/RGB/G", String.valueOf(seekBar.getProgress()));
  203. }
  204. });
  205.  
  206. seek_blau.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
  207. @Override
  208. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  209. if (b){
  210. publish("Haus/Raum4/RGB/B", String.valueOf(i));
  211. blau = i;
  212. }
  213. }
  214.  
  215. @Override
  216. public void onStartTrackingTouch(SeekBar seekBar) {
  217.  
  218. }
  219.  
  220. @Override
  221. public void onStopTrackingTouch(SeekBar seekBar) {
  222. KA = true;
  223. publish("Haus/Raum4/RGB/B", String.valueOf(seekBar.getProgress()));
  224. }
  225. });
  226. }
  227.  
  228. @Override
  229. public void onBackPressed() {
  230. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  231. if (drawer.isDrawerOpen(GravityCompat.START)) {
  232. drawer.closeDrawer(GravityCompat.START);
  233. } else {
  234. super.onBackPressed();
  235. }
  236. }
  237.  
  238. @Override
  239. public boolean onCreateOptionsMenu(Menu menu) {
  240. // Inflate the menu; this adds items to the action bar if it is present.
  241. getMenuInflater().inflate(R.menu.raum4, menu);
  242. return true;
  243. }
  244.  
  245. @Override
  246. public boolean onOptionsItemSelected(MenuItem item) {
  247. // Handle action bar item clicks here. The action bar will
  248. // automatically handle clicks on the Home/Up button, so long
  249. // as you specify a parent activity in AndroidManifest.xml.
  250. int id = item.getItemId();
  251.  
  252. //noinspection SimplifiableIfStatement
  253. if (id == R.id.action_settings) {
  254. return true;
  255. }
  256.  
  257. return super.onOptionsItemSelected(item);
  258. }
  259.  
  260. @SuppressWarnings("StatementWithEmptyBody")
  261. @Override
  262. public boolean onNavigationItemSelected(MenuItem item) {
  263. // Handle navigation view item clicks here.
  264. int id = item.getItemId();
  265.  
  266. if (id == R.id.home) {
  267. Intent wechsel_home = new Intent(this, MainActivity.class);
  268. try {
  269. client.disconnect();
  270. } catch (MqttException e) {
  271. e.printStackTrace();
  272. }
  273. startActivity(wechsel_home);
  274. }
  275.  
  276. else if (id == R.id.raum1) {
  277. Intent wechsel_r1 = new Intent (this, raum1.class);
  278. startActivity(wechsel_r1);
  279. try {
  280. client.disconnect();
  281. } catch (MqttException e) {
  282. e.printStackTrace();
  283. }
  284. }
  285.  
  286. else if (id == R.id.raum2) {
  287. Intent wechsel_r2 = new Intent (this, raum2.class);
  288. //wechsel_r2.putExtra("", client);
  289. try {
  290. client.disconnect();
  291. } catch (MqttException e) {
  292. e.printStackTrace();
  293. }
  294. startActivity(wechsel_r2);
  295. }
  296.  
  297. else if (id == R.id.raum3) {
  298. Intent wechsel_r3 = new Intent (this, raum3.class);
  299. try {
  300. client.disconnect();
  301. } catch (MqttException e) {
  302. e.printStackTrace();
  303. }
  304. startActivity(wechsel_r3);
  305. }
  306.  
  307. else if (id == R.id.raum4) {
  308.  
  309. }
  310.  
  311. else if (id == R.id.raum5) {
  312. Intent wechsel_r4 = new Intent(this, raum4.class);
  313. try {
  314. client.disconnect();
  315. } catch (MqttException e) {
  316. e.printStackTrace();
  317. }
  318. startActivity(wechsel_r4);
  319. }
  320.  
  321. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  322. drawer.closeDrawer(GravityCompat.START);
  323. return true;
  324. }
  325.  
  326. public void updateLicht(){
  327. //tw1.setText("In diesem Raum befinden sich " + personen + " Personen");
  328. if (licht == 1){
  329. btn_helligkeit.getBackground().setColorFilter(0xffffff00, PorterDuff.Mode.MULTIPLY);
  330. }
  331. else if (licht == 0){
  332. btn_helligkeit.getBackground().clearColorFilter();
  333.  
  334. }
  335. }
  336.  
  337. public void connect(){
  338. String clientId = MqttClient.generateClientId();
  339. client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST, clientId);
  340.  
  341. MqttConnectOptions options = new MqttConnectOptions();
  342. options.setUserName(USERNAME);
  343. options.setPassword(PASSWORD.toCharArray());
  344.  
  345. try {
  346. IMqttToken token = client.connect(options);
  347. token.setActionCallback(new IMqttActionListener() {
  348. @Override
  349. public void onSuccess(IMqttToken asyncActionToken) {
  350. Toast.makeText(raum4.this, "connected", Toast.LENGTH_LONG).show();
  351. subscribe();
  352. }
  353.  
  354. @Override
  355. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  356. // Something went wrong e.g. connection timeout or firewall problems
  357. Toast.makeText(raum4.this, "connection failed", Toast.LENGTH_LONG).show();
  358. }
  359. });
  360. } catch (MqttException e) {
  361. e.printStackTrace();
  362. }
  363. }
  364.  
  365. public void publish(String topicStr, String message){
  366. //String topic = "foo/bar";
  367. //String message = "Hallo vom Handy";
  368. byte[] encodedPayload = new byte[0];
  369. try {
  370. client.publish(topicStr, message.getBytes(), QOS, KA);
  371. } catch (MqttException e) {
  372. e.printStackTrace();
  373. }
  374. }
  375.  
  376. public void subscribe(){
  377. client.setCallback(raum4.this);
  378. final String topic = "Haus/Raum4/#";
  379. int qos = 1;
  380. try {
  381. IMqttToken subToken = client.subscribe(String.valueOf(topic), qos);
  382. subToken.setActionCallback(new IMqttActionListener() {
  383. @Override
  384. public void onSuccess(IMqttToken asyncActionToken) {
  385. // The message was published
  386. Toast.makeText(raum4.this, "Successfully subscribed to: " + topic, Toast.LENGTH_SHORT).show();
  387. }
  388.  
  389. @Override
  390. public void onFailure(IMqttToken asyncActionToken,
  391. Throwable exception) {
  392. // The subscription could not be performed, maybe the user was not
  393. // authorized to subscribe on the specified topic e.g. using wildcards
  394. Toast.makeText(raum4.this, "Couldn't subscribe to: " + topic, Toast.LENGTH_SHORT).show();
  395. }
  396. });
  397. } catch (MqttException e) {
  398. e.printStackTrace();
  399. }
  400. }
  401.  
  402. @Override
  403. public void connectionLost(Throwable cause) {
  404.  
  405. }
  406.  
  407. @Override
  408. public void messageArrived(String topic, MqttMessage message) throws Exception {
  409. //Toast.makeText(raum1.this, "Topic: "+topic+"\nMessage: "+message, Toast.LENGTH_LONG).show();
  410. msg = message.toString();
  411. topic1 = topic.toString();
  412. if (topic1.equals("Haus/Raum4/Licht")){
  413. licht = Integer.parseInt(msg);
  414. updateLicht();
  415. }
  416. else if (topic1.equals("Haus/Raum4/Helligkeit")){
  417. int ö;
  418. ö = Integer.parseInt(msg);
  419. seek_helligkeit.setProgress(ö);
  420. }
  421. else if (topic1.equals("Haus/Raum4/RGB/R")){
  422. int ü;
  423. ü = Integer.parseInt(msg);
  424. seek_rot.setProgress(ü);
  425. }
  426. else if (topic1.equals("Haus/Raum4/RGB/G")){
  427. int p;
  428. p = Integer.parseInt(msg);
  429. seek_gruen.setProgress(p);
  430. }
  431. else if (topic1.equals("Haus/Raum4/RGB/B")){
  432. int ä;
  433. ä = Integer.parseInt(msg);
  434. seek_blau.setProgress(ä);
  435. }
  436. updateLicht();
  437. }
  438.  
  439. @Override
  440. public void deliveryComplete(IMqttDeliveryToken token) {
  441.  
  442. }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement