2015-06-24 3 views
-1

Я новичок в студии android и изучаю разработку приложений для Android, У меня есть ошибка, как указано выше, и я не могу ее решить. Любая помощь будет оценена по достоинству. спасибо !!Ошибка: (53, 43) error: не удается найти символьную переменную lumn

Мой ledControl.java код, как следовать

package com.led.led; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.SeekBar; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.ProgressDialog; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.os.AsyncTask; 

import java.io.IOException; 
import java.util.UUID; 


public class ledControl extends ActionBarActivity { 

    Button btnOn, btnOff, btnDis; 
    SeekBar brightness; 
    TextView lumn; 
    String address = null; 
    private ProgressDialog progress; 
    BluetoothAdapter myBluetooth = null; 
    BluetoothSocket btSocket = null; 
    private boolean isBtConnected = false; 
    //SPP UUID. Look for it 
    static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     Intent newint = getIntent(); 
     address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS); //receive the address of the bluetooth device 

     //view of the ledControl 
     setContentView(R.layout.activity_led_control); 

     //call the widgtes 
     btnOn = (Button)findViewById(R.id.button2); 
     btnOff = (Button)findViewById(R.id.button3); 
     btnDis = (Button)findViewById(R.id.button4); 
     brightness = (SeekBar)findViewById(R.id.seekBar); 
     lumn = (TextView)findViewById(R.id.lumn); 

     new ConnectBT().execute(); //Call the class to connect 

     //commands to be sent to bluetooth 
     btnOn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       turnOnLed();  //method to turn on 
      } 
     }); 

     btnOff.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       turnOffLed(); //method to turn off 
      } 
     }); 

     btnDis.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Disconnect(); //close connection 
      } 
     }); 

     brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
       if (fromUser==true) 
       { 
        lumn.setText(String.valueOf(progress)); 
        try 
        { 
         btSocket.getOutputStream().write(String.valueOf(progress).getBytes()); 
        } 
        catch (IOException e) 
        { 

        } 
       } 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 

      } 
     }); 
    } 

    private void Disconnect() 
    { 
     if (btSocket!=null) //If the btSocket is busy 
     { 
      try 
      { 
       btSocket.close(); //close connection 
      } 
      catch (IOException e) 
      { msg("Error");} 
     } 
     finish(); //return to the first layout 

    } 

    private void turnOffLed() 
    { 
     if (btSocket!=null) 
     { 
      try 
      { 
       btSocket.getOutputStream().write("TF".toString().getBytes()); 
      } 
      catch (IOException e) 
      { 
       msg("Error"); 
      } 
     } 
    } 

    private void turnOnLed() 
    { 
     if (btSocket!=null) 
     { 
      try 
      { 
       btSocket.getOutputStream().write("TO".toString().getBytes()); 
      } 
      catch (IOException e) 
      { 
       msg("Error"); 
      } 
     } 
    } 

    // fast way to call Toast 
    private void msg(String s) 
    { 
     Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_led_control, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private class ConnectBT extends AsyncTask<Void, Void, Void> // UI thread 
    { 
     private boolean ConnectSuccess = true; //if it's here, it's almost connected 

     @Override 
     protected void onPreExecute() 
     { 
      progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please wait!!!"); //show a progress dialog 
     } 

     @Override 
     protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background 
     { 
      try 
      { 
       if (btSocket == null || !isBtConnected) 
       { 
        myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device 
        BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available 
        btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection 
        BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); 
        btSocket.connect();//start connection 
       } 
      } 
      catch (IOException e) 
      { 
       ConnectSuccess = false;//if the try failed, you can check the exception here 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine 
     { 
      super.onPostExecute(result); 

      if (!ConnectSuccess) 
      { 
       msg("Connection Failed. Is it a SPP Bluetooth? Try again."); 
       finish(); 
      } 
      else 
      { 
       msg("Connected."); 
       isBtConnected = true; 
      } 
      progress.dismiss(); 
     } 
    } 
} 

И activity_led_control.xml как ниже

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.led.led.ledControl"> 

    <TextView android:text="LED Control" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textview2" /> 

    <SeekBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/seekBar" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Brightness" 
     android:id="@+id/textView3" 
     android:layout_above="@+id/seekBar" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ON" 
     android:id="@+id/button2" 
     android:layout_above="@+id/button3" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="OFF" 
     android:id="@+id/button3" 
     android:layout_above="@+id/button4" 
     android:layout_alignLeft="@+id/button2" 
     android:layout_alignStart="@+id/button2" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Disconnect" 
     android:id="@+id/button4" 
     android:layout_above="@+id/textView3" 
     android:layout_alignLeft="@+id/button3" 
     android:layout_alignStart="@+id/button3" /> 

</RelativeLayout> 

Я знаю, что эта ошибка в отчете текстовое поле, но я не знаю, как разрешить его?

+0

В Wich линии вы получите сообщение об ошибке? – Jens

+0

в java-файле lumn = (TextView) findViewById (R.id.lumn); –

ответ

1

У вас нет TextView, идентифицированного lumn в вашем activity_led_control.xml.

Поэтому эта линия не компилируется:

lumn = (TextView)findViewById(R.id.lumn); 

так R.id.lumn не генерируется.

У вас есть текстовые элементы, идентифицированные "@+id/textview2" и "@+id/textview3". Если вы переименуете один из них на "@+id/lumn", ваш код будет скомпилирован.

+0

, так что я должен удалить это, . Я создал два текстовых файла –

+0

Вы должны переименовать TextViews в xml или в коде. – Eran

+0

какой? Я создал два –

0

Потому что вы не имеете TextView с идентификатором lumn в макете

+0

как решить это? –

+1

set id для вашего текста в виде фонаря. например ** android: id = "@ + id/lumn" ** – Emil

Смежные вопросы