Advertisement
Guest User

Untitled

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